Christopher Klien
Christopher Klien

Reputation: 23

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.

Upvotes: 1

Views: 189

Answers (1)

user330315
user330315

Reputation:

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)'))

Upvotes: 1

Related Questions