Re: Emitting JSON to file using COPY TO

From: Joe Conway <mail(at)joeconway(dot)com>
To: Davin Shearer <davin(at)apache(dot)org>, PostgreSQL-development <pgsql-hackers(at)postgresql(dot)org>, Andrew Dunstan <andrew(at)dunslane(dot)net>
Subject: Re: Emitting JSON to file using COPY TO
Date: 2023-12-04 01:14:31
Message-ID: b604b474-e2b2-4ba0-bc60-fd0cbc05cfc9@joeconway.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general pgsql-hackers

(please don't top quote on the Postgres lists)

On 12/3/23 17:38, Davin Shearer wrote:
> " being quoted as \\" breaks the JSON. It needs to be \".  This has been
> my whole problem with COPY TO for JSON.
>
> Please validate that the output is in proper format with correct quoting
> for special characters. I use `jq` on the command line to validate and
> format the output.

I just hooked existing "row-to-json machinery" up to the "COPY TO"
statement. If the output is wrong (just for for this use case?), that
would be a missing feature (or possibly a bug?).

Davin -- how did you work around the issue with the way the built in
functions output JSON?

Andrew -- comments/thoughts?

Joe

> On Sun, Dec 3, 2023, 10:51 Joe Conway <mail(at)joeconway(dot)com
> <mailto:mail(at)joeconway(dot)com>> wrote:
>
> On 12/3/23 10:31, Davin Shearer wrote:
> > Please be sure to include single and double quotes in the test
> values
> > since that was the original problem (double quoting in COPY TO
> breaking
> > the JSON syntax).
>
> test=# copy (select * from foo limit 4) to stdout (format json);
> {"id":2456092,"f1":"line with ' in it:
> 2456092","f2":"2023-12-03T10:44:40.9712-05:00"}
> {"id":2456093,"f1":"line with \\" in it:
> 2456093","f2":"2023-12-03T10:44:40.971221-05:00"}
> {"id":2456094,"f1":"line with ' in it:
> 2456094","f2":"2023-12-03T10:44:40.971225-05:00"}
> {"id":2456095,"f1":"line with \\" in it:
> 2456095","f2":"2023-12-03T10:44:40.971228-05:00"}
>
> --
> Joe Conway
> PostgreSQL Contributors Team
> RDS Open Source Databases
> Amazon Web Services: https://aws.amazon.com <https://aws.amazon.com>
>

--
Joe Conway
PostgreSQL Contributors Team
RDS Open Source Databases
Amazon Web Services: https://aws.amazon.com

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Davin Shearer 2023-12-04 01:27:49 Re: Emitting JSON to file using COPY TO
Previous Message Davin Shearer 2023-12-03 22:38:28 Re: Emitting JSON to file using COPY TO

Browse pgsql-hackers by date

  From Date Subject
Next Message Michael Paquier 2023-12-04 01:14:36 Re: Is WAL_DEBUG related code still relevant today?
Previous Message Michael Paquier 2023-12-04 01:04:52 Re: PATCH: Add REINDEX tag to event triggers