I have a JSON that looks like this:
"[{""email"": ""AAA"", ""updated_at"": ""AAA"", ""net_income_verified"": AAA}, {""email"": ""BBB"", ""updated_at"": ""BBB"", ""net_income_verified"": BBB}, {""email"": ""CCC"", ""updated_at"": ""CCC"", ""net_income_verified"": CCC}]"
And I would like to get all the data from all the fields so it ends up looking like this:
user_id | updated_at | net_income_verified | |
---|---|---|---|
1 | AAA | AAA | AAA |
1 | BBB | BBB | BBB |
1 | CCC | CCC | CCC |
There are multiple user_ids so the number of entries in the json field could vary.
I tried going one opr one for each user_id, but since the number of entries can vary, I don’t know exactly how many entries I should try.
Edit: Each user_id has a Json, so far my query loks like this:
SELECT user_id, json_field
FROM users
2
Answers
You can use jsonb_array_elements function to extract all entries from the list.
However I do not see where
user_id
attribute could come from.Edit
Given that every record has a JSON field and
user_id
then the query looks a bit different. I would call this the "old school approach", first flatten (using a lateral join here) and then query. Thet
CTE is a mimic of the actual table.There’s a
json_populate_recordset()
function for that exact purpose: demoYou first set up an table/type showing what structure you want your
json
to be mapped to, then give the function an example record of that table/type along with the targetjson
value. It handles it all for you.