Simo Kivistö's answer works if you are sure that the $ character or any other quotation mark character you have selected does not appear on your lines. In my case, I had to export a very large table, and there was no special character that did not appear in the rows.
To get around this problem, I passed the output of the COPY to sed to return double-escaped quotation marks:
psql -c "COPY (SELECT row_to_json(t) from my_table as t) to STDOUT;" | sed 's/\\"/\"/g' > my_table.json
I am expressing a sed expression that simply replaces the occurrences of \\" with \" .
source share