简体   繁体   中英

How to filter the JSON on boolean value

{
  "L1": [
    {
      "f1": "446",
      "f2": true,
      "f3": true
    },
    {
      "f1": "191",
      "f2": true,
      "f3": true
    }
  ]
}

I want to filter where f2 is true and f3 is true in PostgreSQL.

You can use a JSON path query to get all elements where both keys are true and then compare that to the total length of the array:

select ...
from the_table
where jsonb_array_length(the_column -> 'L1') = jsonb_array_length(jsonb_path_query_array(the_column, '$.L1[*] ? (@.f2 == true && @.f3 == true)'))

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