Re: Emitting JSON to file using COPY TO
Andrew Dunstan <andrew@dunslane.net>
From: Andrew Dunstan <andrew@dunslane.net>
To: Joe Conway <mail@joeconway.com>, Davin Shearer <davin@apache.org>,
PostgreSQL-development <pgsql-hackers@postgresql.org>
Date: 2023-12-04T14:25:04Z
Lists: pgsql-hackers
Commits
Same data as JSON:
GET /api/v1/messages/:b64id/commits
the thread's linked commits as JSON, with link sources.
API reference →
-
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
On 2023-12-04 Mo 08:37, Joe Conway wrote: > On 12/4/23 07:41, Andrew Dunstan wrote: >> >> On 2023-12-03 Su 20:14, Joe Conway wrote: >>> (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? >> >> I meant to mention this when I was making comments yesterday. >> >> The patch should not be using CopyAttributeOutText - it will try to >> escape characters such as \, which produces the effect complained of >> here, or else we need to change its setup so we have a way to inhibit >> that escaping. > > > Interesting. > > I am surprised this has never been raised as a problem with COPY TO > before. > > Should the JSON output, as produced by composite_to_json(), be sent > as-is with no escaping at all? If yes, is JSON somehow unique in this > regard? Text mode output is in such a form that it can be read back in using text mode input. There's nothing special about JSON in this respect - any text field will be escaped too. But output suitable for text mode input is not what you're trying to produce here; you're trying to produce valid JSON. So, yes, the result of composite_to_json, which is already suitably escaped, should not be further escaped in this case. cheers andrew -- Andrew Dunstan EDB: https://www.enterprisedb.com