I have a jsonb column with data like this:
{
"rules": [
{
"type": "weekdays",
"values": [
3,
5
]
},
{
"type": "dates",
"values": [
"2024-01-01",
"2024-01-02",
"2024-01-03"
]
}
]
}
What would a SELECT statement look like if I:
- Wanted to get all records WHERE ( rules[0].type.weekdays ).values CONTAINS 3:
SELECT * FROM table WHERE jsonb_column->>'rules'->0->'type'->'weekdays' : [the-result]->values = ANY ('{3}'::int[]);
- Wanted to get all records WHERE ( rules[0].type.dates ).values CONTAINS ‘2024-01-02’ (date-string):
SELECT * FROM table WHERE jsonb_column->>'rules'->0->'type'->'dates' ? [the-result]->values = ANY ('{"2024-01-03"}'::date[?]);
So, basically I need to find the object of a certain type AND THEN figure out whether that objects values contain what I’m looking for.
PSEUDO CODE:
SELECT * FROM table WHERE jsonb_column->rules[ how-to-iterate-array? ]->type = 'weekdays' AND [from-that-resulting-object]->>values CONTAIN 3;
2
Answers
I believe you’re looking for json paths which let you express such conditions. In your case, that would be
or
you can use
jsonpath
andjsonb_path_exists
for that:returns only one row: