3

I am trying to write a SQL query that cross joins each row with its own JSON array elements. Let's say this is the data we have (I know, it doesn't make much sense):

| id | name | info                                            |
|----|------|-------------------------------------------------|
| 1  | john | [{score: 20, point: 10},{score: 25, point: 15}] |
| 2  | jane | [{score: 25, point: 15},{score: 35, point: 45}] |

What I am trying to get as an end result looks like this:

| id | name | score | point |
|----|------|-------|-------|
| 1  | john | 20    | 10    |
| 1  | john | 25    | 15    |
| 2  | jane | 25    | 15    |
| 2  | jane | 35    | 45    |

How can I write a query that does the job? I don't have the grants to create a new function so this has to be purely select statements, not plpgsql stuff.

1 Answer 1

1

You can unnest with a lateral join and json[b]_array_elements:

select t.id, t.name, i.obj ->> 'score' as score, i.obj ->> 'point' as point
from mytable t
cross join lateral jsonb_array_elements(t.info) i(info)
Sign up to request clarification or add additional context in comments.

2 Comments

This is incredible. How can I read more about this technique? The stuff I found for lateral joins is very complicated and I couldn't get any of them to work, this one is so simple. I really want to learn what some of these mean, especially i(info) notation
@ÖzençB.: the documentatiion is a good place to start. See section "7.2.1.5. LATERAL Subqueries".

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.