I have a table master_data_approval_table in my postgres DB having field approval_value (type Jsonb) which contains below structure:
We want only those records where each object of jsonb array strictly matches the given condition "status": "Approved"
Expected O/P:
I tried with different queries but didn’t find the exact query to get the expected output. Please help me with this.
Thanks in advance.
2
Answers
Here’s what you want:
SELECT * FROM master_data_approval_table
WHERE (approval_value::jsonb)->>'status' = 'Approved'
For some reason if you try to select
json_field->>'json_object'
you get an error, so you need to (somewhat redundantly) cast your json field to json.First flatten the JSONB field and then do a routine select.
I assume that master_data_approval_table.id is the primary key. If not then replace
select mdat.*
withselect mdat.id
in the query above.DB Fiddle.