Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1rAzf2-0091Tb-6M for pgsql-hackers@arkaria.postgresql.org; Wed, 06 Dec 2023 21:42:28 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.94.2) (envelope-from ) id 1rAzf0-00CnNB-TQ for pgsql-hackers@arkaria.postgresql.org; Wed, 06 Dec 2023 21:42:26 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1rAzf0-00CnN3-FN for pgsql-hackers@lists.postgresql.org; Wed, 06 Dec 2023 21:42:26 +0000 Received: from mail-pg1-x529.google.com ([2607:f8b0:4864:20::529]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.94.2) (envelope-from ) id 1rAzex-0098vq-OJ for pgsql-hackers@postgresql.org; Wed, 06 Dec 2023 21:42:25 +0000 Received: by mail-pg1-x529.google.com with SMTP id 41be03b00d2f7-5c659db0ce2so196072a12.0 for ; Wed, 06 Dec 2023 13:42:23 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=jackdb.com; s=google; t=1701898943; x=1702503743; darn=postgresql.org; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:from:to:cc:subject:date:message-id:reply-to; bh=zATtR6m7RQIGVRgZad/SAhczCabIb1pSiU3YJMhtS9k=; b=IRRXGIhSgrk0jSKLdLEAnNQW1Gn65TrmMpQYT+sA9XRSSKV1JjpB5+vuiYEh8v/W+J +7mBA2Be1SeoJKq5PitrzOoVZers3RdEyZp6U9p3ntd3Fpsqqs6Wbt7821nbcVrYzWf/ 3n9ZYIjfyM6Ahu3Z8jHn9NNcnR6LNcJfvRdNM= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1701898943; x=1702503743; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=zATtR6m7RQIGVRgZad/SAhczCabIb1pSiU3YJMhtS9k=; b=auHig/+91gPyRYZu1sfXzm94TX0QrSMo5sObLAgB7O5OjODeF7XJ1ZCzPQPhQqNwwx whjiXd0JtSigmgIfmy9RoxbtLVivfBCThM2zXRP41fbmnUuXCOhmWTN4+lDpMqWDGXBz KlirJHC2PtRaA8ah9bIdVWYxbamNIPC8DYaMEihM1QaCDn4GPEgNHGkakCZsjpi/CcQF AslvfzI5FITrH/Pj0NX1Q255cJxbCX9QBuO0P4pOzOKDyGop/ZyFGM765A1UdnmWfKk7 1D9Tl3JqJ/iwiaBzO1KgRIMz0mQjnPy0qseDPYowcEHc54VyGtRHw39EHwmBVdOdzCj+ dfwg== X-Gm-Message-State: AOJu0YxMAMtnpKSfHNouItkBJCVoI9rH4t/4PrASebTCUdd3xgFPspiI wuFpIeE0XbTmmRVSOfBEqYZmbFXE/8jvj0g38Wwoay2vCVlCNzGWuJI= X-Google-Smtp-Source: AGHT+IHYrWOKGnIdKpTVg2wyChn8k+ydmeLrfV7Mir687VltH0l2fJviR8GCf9afKd7IbQrBmv4GqD5k18QEnkecghE= X-Received: by 2002:a05:6a20:8fa5:b0:18f:cbdd:8102 with SMTP id k37-20020a056a208fa500b0018fcbdd8102mr1463641pzj.103.1701898942760; Wed, 06 Dec 2023 13:42:22 -0800 (PST) MIME-Version: 1.0 References: <5c84b70b-ba18-c45d-dbbe-612fa229b2ce@dunslane.net> <398c22f6-4299-4b17-80bf-2f14f4afd592@joeconway.com> <46cc4507-a0d9-4044-b2ce-5a8bca8015c0@joeconway.com> <2554e520-e103-8978-dcb5-807dfeb77402@dunslane.net> <926ff917-8371-40ec-b5e6-ab7b0e09bdc5@joeconway.com> <315b81d4-4b67-7828-0355-3808cd14acd1@dunslane.net> <7a60faf6-e7f1-419d-aee6-10a78ea2fe81@joeconway.com> <2e7ff718-895d-83fc-46f7-be25e23b23b4@dunslane.net> <1104915.1701877459@sss.pgh.pa.us> <19a5f9d8-bd1f-9e51-0f5b-510c1189a8a7@dunslane.net> <4d5688f4-9582-4093-8448-e1867bc9e2bc@joeconway.com> In-Reply-To: <4d5688f4-9582-4093-8448-e1867bc9e2bc@joeconway.com> From: Sehrope Sarkuni Date: Wed, 6 Dec 2023 16:42:11 -0500 Message-ID: Subject: Re: Emitting JSON to file using COPY TO To: Joe Conway Cc: Andrew Dunstan , Tom Lane , Davin Shearer , PostgreSQL-development Content-Type: multipart/alternative; boundary="000000000000ed59aa060bde391c" List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --000000000000ed59aa060bde391c Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable On Wed, Dec 6, 2023 at 4:29=E2=80=AFPM Joe Conway wrot= e: > > 1. Outputting a top level JSON object without the additional column > > keys. IIUC, the top level keys are always the column names. A common us= e > > case would be a single json/jsonb column that is already formatted > > exactly as the user would like for output. Rather than enveloping it in > > an object with a dedicated key, it would be nice to be able to output i= t > > directly. This would allow non-object results to be outputted as well > > (e.g., lines of JSON arrays, numbers, or strings). Due to how JSON is > > structured, I think this would play nice with the JSON lines v.s. array > > concept. > > > > COPY (SELECT json_build_object('foo', x) AS i_am_ignored FROM > > generate_series(1, 3) x) TO STDOUT WITH (FORMAT JSON, > > SOME_OPTION_TO_NOT_ENVELOPE) > > {"foo":1} > > {"foo":2} > > {"foo":3} > > Your example does not match what you describe, or do I misunderstand? I > thought your goal was to eliminate the repeated "foo" from each row... > The "foo" in this case is explicit as I'm adding it when building the object. What I was trying to show was not adding an additional object wrapper / envelope. So each row is: {"foo":1} Rather than: "{"json_build_object":{"foo":1}} If each row has exactly one json / jsonb field, then the user has already indicated the format for each row. That same mechanism can be used to remove the "foo" entirely via a json/jsonb array. Regards, -- Sehrope Sarkuni Founder & CEO | JackDB, Inc. | https://www.jackdb.com/ --000000000000ed59aa060bde391c Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable
On Wed, Dec 6, 2023 at 4:29=E2=80=AFPM Joe Con= way <mail@joeconway.com> wr= ote:
> 1. Outputting a top level JSON= object without the additional column
> keys. IIUC, the top level keys are always the column names. A common u= se
> case would be a single json/jsonb column that is already formatted > exactly as the user would like for output. Rather than enveloping it i= n
> an object with a dedicated key, it would be nice to be able to output = it
> directly. This would allow non-object results to be outputted as well =
> (e.g., lines of JSON arrays, numbers, or strings). Due to how JSON is =
> structured, I think this would play nice with the JSON lines v.s. arra= y
> concept.
>
> COPY (SELECT json_build_object('foo', x) AS i_am_ignored FROM =
> generate_series(1, 3) x) TO STDOUT WITH (FORMAT JSON,
> SOME_OPTION_TO_NOT_ENVELOPE)
> {"foo":1}
> {"foo":2}
> {"foo":3}

Your example does not match what you describe, or do I misunderstand? I thought your goal was to eliminate the repeated "foo" from each r= ow...

The "foo" in this = case is explicit as I'm adding it when building the object. What I was = trying to show was not adding an additional object wrapper / envelope.

So each row is:

{"foo&qu= ot;:1}

Rather than:

"= ;{"json_build_object":{"foo":1}}

If each row has exactly one json / jsonb field, then the user has alread= y indicated the format for each row.

That sa= me mechanism can be used to remove the "foo" entirely via a json/= jsonb array.

Regards,
-- Seh= rope Sarkuni
Founder & CEO | Jack= DB, Inc. |=C2=A0https= ://www.jackdb.com/

=C2=A0
--000000000000ed59aa060bde391c--