pg.ddx.io pgsql-general@postgresql.org mailing list archive
help / color / mirror / Atom feedJSON fields with backslashes
4+ messages / 4 participants
[nested] [flat]
* JSON fields with backslashes
@ 2023-10-13 21:51 Greig Wise <greigwise@comcast.net>
0 siblings, 2 replies; 4+ messages in thread
From: Greig Wise @ 2023-10-13 21:51 UTC (permalink / raw)
To: pgsql-general
Hello. I have run into an issue when using the copy command on tables with json columns where Postgres seems to improperly escape backslashes under certain conditions thus creating invalid JSON. Here is an example I have of the behavior:
create table test_json(json_data json);
insert into test_json values ('{"test1": "2011-01-01", "description":"test\ntest2\ntest3 test''s\n \"Quoted Phrase\" test\ntest."}’);
copy test_json to '/var/tmp/t.json’;
cat /var/tmp/t.json
{"test1": "2011-01-01", "description":"test\\ntest2\\ntest3 test's\\n \\"Quoted Phrase\\" test\\ntest."}
Note that the quotes within the json field have \\ in front, thus negating the escape of the quotes around “Quoted Phrase”. Which then renders the whole thing invalid JSON. Is this a bug?
I am using PostgreSQL V12.16.
Thanks,
Greig Wise
^ permalink raw reply [nested|flat] 4+ messages in thread
* Re: JSON fields with backslashes
@ 2023-10-13 22:01 David G. Johnston <david.g.johnston@gmail.com>
parent: Greig Wise <greigwise@comcast.net>
1 sibling, 1 reply; 4+ messages in thread
From: David G. Johnston @ 2023-10-13 22:01 UTC (permalink / raw)
To: Greig Wise <greigwise@comcast.net>; +Cc: pgsql-general
On Fri, Oct 13, 2023 at 2:53 PM Greig Wise <greigwise@comcast.net> wrote:
> Hello. I have run into an issue when using the copy command on tables
> with json columns where Postgres seems to improperly escape backslashes
> under certain conditions thus creating invalid JSON. Here is an example I
> have of the behavior:
>
> create table test_json(json_data json);
> insert into test_json values ('{"test1": "2011-01-01",
> "description":"test\ntest2\ntest3 test''s\n \"Quoted Phrase\"
> test\ntest."}’);
> copy test_json to '/var/tmp/t.json’;
>
> cat /var/tmp/t.json
> {"test1": "2011-01-01", "description":"test\\ntest2\\ntest3 test's\\n
> \\"Quoted Phrase\\" test\\ntest."}
>
> Note that the quotes within the json field have \\ in front, thus negating
> the escape of the quotes around “Quoted Phrase”. Which then renders the
> whole thing invalid JSON. Is this a bug?
>
COPY doesn't output JSON, it outputs csv/tsv structured text. In that
format the described output is correct. If you need a different output
format you need to use a different tool. Ideally you can just get the JSON
into whatever client software you are writing with and export it from
there. Doing it in psql is possible but a bit tricky. Doing it within the
server usually isn't worth the hassle.
David J.
^ permalink raw reply [nested|flat] 4+ messages in thread
* Re: JSON fields with backslashes
@ 2023-10-13 22:01 Tom Lane <tgl@sss.pgh.pa.us>
parent: Greig Wise <greigwise@comcast.net>
1 sibling, 0 replies; 4+ messages in thread
From: Tom Lane @ 2023-10-13 22:01 UTC (permalink / raw)
To: Greig Wise <greigwise@comcast.net>; +Cc: pgsql-general
Greig Wise <greigwise@comcast.net> writes:
> Hello. I have run into an issue when using the copy command on tables
> with json columns where Postgres seems to improperly escape backslashes
> under certain conditions thus creating invalid JSON.
AFAICS this is COPY's normal behavior: it doubles backslashes in
the default format. You could use CSV format, perhaps, but that
has its own quoting rules that also mean that what comes out is
not going to be json-and-nothing-but. See "File Formats" here:
https://www.postgresql.org/docs/current/sql-copy.html
regards, tom lane
^ permalink raw reply [nested|flat] 4+ messages in thread
* Re: JSON fields with backslashes
@ 2023-10-14 00:09 Erik Wienhold <ewie@ewie.name>
parent: David G. Johnston <david.g.johnston@gmail.com>
0 siblings, 0 replies; 4+ messages in thread
From: Erik Wienhold @ 2023-10-14 00:09 UTC (permalink / raw)
To: David G. Johnston <david.g.johnston@gmail.com>; +Cc: Greig Wise <greigwise@comcast.net>; pgsql-general
On 2023-10-14 00:01 +0200, David G. Johnston write:
> On Fri, Oct 13, 2023 at 2:53 PM Greig Wise <greigwise@comcast.net> wrote:
>
> > Hello. I have run into an issue when using the copy command on tables
> > with json columns where Postgres seems to improperly escape backslashes
> > under certain conditions thus creating invalid JSON. Here is an example I
> > have of the behavior:
> >
> > create table test_json(json_data json);
> > insert into test_json values ('{"test1": "2011-01-01",
> > "description":"test\ntest2\ntest3 test''s\n \"Quoted Phrase\"
> > test\ntest."}’);
> > copy test_json to '/var/tmp/t.json’;
> >
> > cat /var/tmp/t.json
> > {"test1": "2011-01-01", "description":"test\\ntest2\\ntest3 test's\\n
> > \\"Quoted Phrase\\" test\\ntest."}
> >
> > Note that the quotes within the json field have \\ in front, thus negating
> > the escape of the quotes around “Quoted Phrase”. Which then renders the
> > whole thing invalid JSON. Is this a bug?
> >
>
> COPY doesn't output JSON, it outputs csv/tsv structured text. In that
> format the described output is correct. If you need a different output
> format you need to use a different tool. Ideally you can just get the JSON
> into whatever client software you are writing with and export it from
> there. Doing it in psql is possible but a bit tricky. Doing it within the
> server usually isn't worth the hassle.
in psql:
\pset format unaligned
\pset tuples_only
\o /var/tmp/t.json
select json_data from test_json limit 1;
--
Erik
^ permalink raw reply [nested|flat] 4+ messages in thread
end of thread, other threads:[~2023-10-14 00:09 UTC | newest]
Thread overview: 4+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2023-10-13 21:51 JSON fields with backslashes Greig Wise <greigwise@comcast.net>
2023-10-13 22:01 ` David G. Johnston <david.g.johnston@gmail.com>
2023-10-14 00:09 ` Erik Wienhold <ewie@ewie.name>
2023-10-13 22:01 ` Tom Lane <tgl@sss.pgh.pa.us>
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox