I have a database table like the following:
---------------------------------------
| id | json_array (jsonb) |
---------------------------------------
| 1 | [{"a": 1}, {"a": 5}, {"a": 1}] |
| 2 | [{"a": 2}] |
| 3 | [{"a": 1}] |
---------------------------------------
I want to use PostgreSQL's JSON query abilities to select certain sub dictionaries in the json_array
array, e.g. dictionaries where "a": 1
.
The output should be
------------------
| id | json_dict |
------------------
| 1 | {"a": 1} |
| 1 | {"a": 1} |
| 3 | {"a": 1} |
------------------
The following query works for the first element in each array, but I want to check for all of them:
SELECT id, json_array -> 0
FROM my_table
WHERE json_array -> 0 -> "a" = 1;