Skip to content
Advertisement

Querying nested Json object in postgres

I have a jsonb column in my table and data is in this format:

I would like to get the count of rows in which key = value1. I tried some queries like:

It returned 0 rows, though there are rows in db with that key value pair. Thanks in advance,

Advertisement

Answer

Use jsonb_array_elements() for the column summary as it is in the form of json array.

Alternatively, you can get rid of the function using @> operator:

The second solution should be faster.

Db<>fiddle.

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