DEV Community

Dino for Wizard Health

Posted on

Meza: From PostgreSQL table to a JSON file

Ever wanted to export data from the PostgreSQL table into a JSON file?

👇

pg_cmd="\\copy (select row_to_json(r) from (select * FROM table_name) r ) to
table.json"
psql -w -h host -U username -d database_name -c $pg_cmd
sed -i -e 's/\\\\/\\/g' table.json # clean up extra '\' characters

Enter fullscreen mode Exit fullscreen mode

After executing these commands you will have two JSON files

  1. table.json - initial JSON exported data file
  2. table.json-e - file which contains cleaned up JSON, without extra \ characters

✌️

Top comments (0)