Skip to content
Advertisement

Transform JSON rows into PostgreSQL Table

I have a table in my database that returns me some columns. One of them is a list of JSONs that are, in fact, other tables, but in JSON format. The image belows show an example.

Example of database in pgAdmin

I want to transform each row of that column “data” into separate tables, because I need specific information inside each JSON. How can I do that using only PostgreSQL?

Advertisement

Answer

You can use below query to create transfer your data (json data column) into a new table

insert into json_table
SELECT p.*
from test_json t
cross join lateral json_populate_record (NULL::json_table, t.data ::json) as p;

Here json_table is the new table in which data will be stored, test_json is your table in which json data is stored in data column. Replace those with your table names and you are good to go.

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement