Home > Software engineering >  Json Arrays of objects PostgreSQL Table format
Json Arrays of objects PostgreSQL Table format

Time:05-21

I have a JSON file (array of objects) which I have to convert into a table format using a PostgreSQL query.

Follow Sample Data. "b", "c", "d", "e" are to be extracted as separate tables as they are arrays and in these arrays, there are objects

I have tried using json_populate_recordset() but it only works if I have a single array.

[{a:"1",b:"2"},{a:"10",b:"20"}]

I have referred to some links and codes.

Column4

The 'tablename from column' in the json_populate_recordset function, is the table we are trying to extract followed with b its columns and datatypes. input for json function

WITH input AS(
   SELECT cast(column as json) as a
   FROM tablename
)
SELECT b.*
FROM input c,
            json_populate_recordset(NULL::record,c.a->'tablename from column') as b(columnname1 datatype, columnname2 datatype)
  • Related