简体   繁体   中英

trouble filtering jsonb data in postgresql

I'm trying to learn how to filter jsonb data. When I run:

SELECT DISTINCT jsonb_array_elements(data) 
FROM reports 
WHERE data @> '[{"status": "Active"}]'

Instead of only returning rows where status: Active it seems to ignore the WHERE clause and I also return rows that contain status: Inactive

Looking for some help understanding what is going on here.

The data looks like

[{"report": "Report1", "status": "Active"},
 {"report": "Report2", "status": "Inactive"},
 {"report": "Report3", "status": "Inactive"},
 {"report": "Report4", "status": "Active"}]
select * from reports
join lateral jsonb_array_elements(data) j(v)
on true
where
v->>'status' = 'Active'

here v is elements from jsonb array, eg {"report": "Report1", "status": "Active"}

Then filter 'status' keys, where value is 'Active', using ->> operator

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM