postgresql - Postgres row_to_json produces invalid JSON with double escaped quotes -
postgres escapes quotes incorrectly when creating json export. note double quotes in below update...
update models set column='"hello"' id=1; copy (select row_to_json(models) (select column shaders id=1) shaders) '/output.json';
the contents of output.json:
{"column":"\\"hello\\""}
you can see quotes escaped improperly , creates invalid json. should be:
{"column":"\"hello\""}
how can fix postgres bug or work around it?
this not json related. it's way text format (default) in copy
command handles backslashes. the postgresql documentation - copy:
backslash characters (
\
) can used in copy data quote data characters might otherwise taken row or column delimiters. in particular, following characters must preceded backslash if appear part of column value: backslash itself, newline, carriage return, , current delimiter character.
(emphasis mine.)
can solve using csv-format , changing quote character doublequote else.
to demonstrate:
select row_to_json(row('"hello"')) | "{"f1":"\"hello\""}" |
copy (select row_to_json(row('"hello"'))) '/output.json'; | {"f1":"\\"hello\\""} |
copy (select row_to_json(row('"hello"'))) '/output.json' csv quote '$'; | {"f1":"\"hello\""} |
Comments
Post a Comment