Re: Emitting JSON to file using COPY TO
jian he <jian.universality@gmail.com>
From: jian he <jian.universality@gmail.com>
To: Florents Tselai <florents.tselai@gmail.com>
Cc: Junwang Zhao <zhjwpku@gmail.com>, Joe Conway <mail@joeconway.com>, "Andrey M. Borodin" <x4mmm@yandex-team.ru>,
Dean Rasheed <dean.a.rasheed@gmail.com>, Daniel Verite <daniel@manitou-mail.org>,
Andrew Dunstan <andrew@dunslane.net>, Davin Shearer <davin@apache.org>,
PostgreSQL-development <pgsql-hackers@postgresql.org>
Date: 2026-02-06T03:26:04Z
Lists: pgsql-hackers, pgsql-general
Attachments
- v23-0002-json-format-for-COPY-TO.patch (text/x-patch) patch v23-0002
- v23-0003-Add-option-force_array-for-COPY-JSON-FORMAT.patch (text/x-patch) patch v23-0003
- v23-0001-introduce-CopyFormat-refactor-CopyFormatOptions.patch (text/x-patch) patch v23-0001
On Wed, Feb 4, 2026 at 12:41 AM Florents Tselai
<florents.tselai@gmail.com> wrote:
>
> I (and others I assume) would really like to see this in 19;
> glancing at the thread above and in the test cases I see this is in good shape for comitter review.
> No?
>
> If I were to add something it would be an example in copy.sgml
> <para>
> When the <literal>FORCE_ARRAY</literal> option is enabled,
> the entire output is wrapped in a JSON array and individual rows are separated by commas:
> <programlisting>
> COPY (SELECT id, name FROM users) TO STDOUT (FORMAT JSON, FORCE_ARRAY);
> </programlisting>
> <programlisting>
> [
> {"id": 1, "name": "Alice"}
> ,{"id": 2, "name": "Bob"}
> ,{"id": 3, "name": "Charlie"}
> ]
> </programlisting>
> </para>
>
v23-0003-Add-option-force_array-for-COPY-JSON-FORMAT.patch
I've added:
+<para>
+ When the <literal>FORCE_ARRAY</literal> option is enabled,
+ the entire output is wrapped in a single JSON array with rows
separated by commas:
+<programlisting>
+COPY (SELECT * FROM (VALUES(1),(2)) val(id)) TO STDOUT (FORMAT JSON,
FORCE_ARRAY);
+</programlisting>
+The output is as follows:
+<screen>
+[
+ {"id":1}
+,{"id":2}
+]
+</screen>
+</para>
+
+
> Also, apologies if that has been discussed already,
> is there a good reason why didn't we just go with a simple "WRAP_ARRAY" ?
>
I don’t have a particular preference.
If the consensus is that WRAP_ARRAY is better than FORCE_ARRAY, we can
change it accordingly.
--
jian
https://www.enterprisedb.com/
Commits
-
Add option force_array for COPY JSON FORMAT
- 4c0390ac53b7 19 (unreleased) landed
-
json format for COPY TO
- 7dadd38cda95 19 (unreleased) landed
-
introduce CopyFormat, refactor CopyFormatOptions
- a2145605ee3d 19 (unreleased) landed
-
Doc: add IDs to copy.sgml's <varlistentry> and <refsect1>
- e4018f891dec 19 (unreleased) cited
-
Refactor COPY TO to use format callback functions.
- 2e4127b6d2d8 18.0 cited