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 1r8MZ3-009UF0-Ug for pgsql-general@arkaria.postgresql.org; Wed, 29 Nov 2023 15:33:25 +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 1r8MZ2-00BVxI-Gb for pgsql-general@arkaria.postgresql.org; Wed, 29 Nov 2023 15:33:24 +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 1r8MZ2-00BVxA-5K for pgsql-general@lists.postgresql.org; Wed, 29 Nov 2023 15:33:24 +0000 Received: from mxout1-ec2-va.apache.org ([3.227.148.255]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1r8MYv-007xVq-Ah for pgsql-general@lists.postgresql.org; Wed, 29 Nov 2023 15:33:22 +0000 Received: from mail.apache.org (mailgw-he-de.apache.org [116.203.246.181]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) (No client certificate requested) by mxout1-ec2-va.apache.org (ASF Mail Server at mxout1-ec2-va.apache.org) with ESMTPS id 842954E577 for ; Wed, 29 Nov 2023 15:33:08 +0000 (UTC) Received: (qmail 4088057 invoked by uid 116); 29 Nov 2023 15:33:07 -0000 Received: from mailrelay1-he-de.apache.org (HELO mailrelay1-he-de.apache.org) (116.203.21.61) by apache.org (qpsmtpd/0.94) with ESMTP; Wed, 29 Nov 2023 15:33:07 +0000 Authentication-Results: apache.org; auth=none Received: from mail-pg1-f170.google.com (mail-pg1-f170.google.com [209.85.215.170]) by mailrelay1-he-de.apache.org (ASF Mail Server at mailrelay1-he-de.apache.org) with ESMTPSA id 2B5B640EF4 for ; Wed, 29 Nov 2023 15:33:07 +0000 (UTC) Received: by mail-pg1-f170.google.com with SMTP id 41be03b00d2f7-5ab94fc098cso4155024a12.1 for ; Wed, 29 Nov 2023 07:33:07 -0800 (PST) X-Gm-Message-State: AOJu0YyTSQMNA0ckBwDZTli1oFqEmtzBfLpELG4dvaRssxX+fUZsItms CiRFFN9ZMBcz5AgOLJ4En27iJyp/DtIovWvWDIk= X-Google-Smtp-Source: AGHT+IFapXk6sHIf3Vw0vXjyVez/84wPPJGrmEcKd2ZdG5pEBomxsWfDycHtvZFQ7RwMFc+ssqjM2d25LGvwl9tu29A= X-Received: by 2002:a17:90a:b946:b0:285:a734:f0e with SMTP id f6-20020a17090ab94600b00285a7340f0emr12134642pjw.39.1701271985521; Wed, 29 Nov 2023 07:33:05 -0800 (PST) MIME-Version: 1.0 References: <3853387.1701096982@sss.pgh.pa.us> <3a98decf-3fe3-4b49-9b68-fda01338872c@sedlakovi.org> In-Reply-To: <3a98decf-3fe3-4b49-9b68-fda01338872c@sedlakovi.org> From: Davin Shearer Date: Wed, 29 Nov 2023 10:32:54 -0500 X-Gmail-Original-Message-ID: Message-ID: Subject: Re: Emitting JSON to file using COPY TO To: "pgsql-general@lists.postgresql.org" Content-Type: multipart/alternative; boundary="0000000000005d0d50060b4c40d7" List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --0000000000005d0d50060b4c40d7 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Thanks for the responses everyone. I worked around the issue using the `psql -tc` method as Filip described. I think it would be great to support writing JSON using COPY TO at some point so I can emit JSON to files using a PostgreSQL function directly= . -Davin On Tue, Nov 28, 2023 at 2:36=E2=80=AFAM Filip Sedl=C3=A1k wrote: > This would be a very special case for COPY. It applies only to a single > column of JSON values. The original problem can be solved with psql > --tuples-only as David wrote earlier. > > > $ psql -tc 'select json_agg(row_to_json(t)) > from (select * from public.tbl_json_test) t;' > > [{"id":1,"t_test":"here's a \"string\""}] > > > Special-casing any encoding/escaping scheme leads to bugs and harder > parsing. > > Just my 2c. > > -- > Filip Sedl=C3=A1k > --0000000000005d0d50060b4c40d7 Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable
Thanks for the responses everyone.

I wo= rked around the issue using the `psql -tc` method as Filip described.
=

I think it would be great to support writing JSON using= COPY TO at some=C2=A0point so=C2=A0I can emit JSON to=C2=A0files using a P= ostgreSQL=C2=A0function directly.

-Davin

On T= ue, Nov 28, 2023 at 2:36=E2=80=AFAM Filip Sedl=C3=A1k <filip@sedlakovi.org> wrote:
This would be a very special case for COPY. It applies only to a si= ngle
column of JSON values. The original problem can be solved with psql
--tuples-only as David wrote earlier.


$ psql -tc 'select json_agg(row_to_json(t))
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 from (select * from public= .tbl_json_test) t;'

=C2=A0 [{"id":1,"t_test":"here's a \"stri= ng\""}]


Special-casing any encoding/escaping scheme leads to bugs and harder
parsing.

Just my 2c.

--
Filip Sedl=C3=A1k
--0000000000005d0d50060b4c40d7--