Skip to content
Advertisement

Query based on JSON object values

If I have a column(lets call it students) in a PostgreSQL db in a JSON/JSONB format like such:

How can I query the students who have passed?

Ideally, I’d write something like SELECT (students->'student_id') FROM table_name WHERE (students->'Pass')=true, but that doesn’t work because it has the missing layer of keys i.e “1”, “2” and “3”. Something that has its logic similar to ...WHERE (students-> *All Keys* ->'Pass')=true is what I’m looking for.

Advertisement

Answer

Sample data

Variant 1 (using extract key value)

Variant 2 (using SQL/JSON)

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