there is in postgres (v 9.5, if it is matter):
create table json_test(
id varchar NOT NULL,
data jsonb NOT NULL,
PRIMARY KEY(id)
);
Where data is json and contains array of arrays
{
"attribute": "0",
"array1": [{
"id": "a12",
"attribute": "1",
"array2": [{
"id": "a21",
"attribute": "21"
}]
},
{
"id": "a12",
"attribute": "2",
"array2": [{
"id": "22",
"attribute": "22"
}]
}]
}
Required:
select id from json_test where
json_test->>'attribute'='0' and
array1.[id='a12'].array2.attribute='22'
Query should mean: give me all ids where
- some top level attributes have particular values
- particular object in array has required attributes
- some object (from array2) in particular array1 has required attributes
the trick is how to implement the last condition.
another example:
{
"attribute": "0",
"array1": [{
"id": "a12",
"attribute": "1",
"array2": [{
"id": "a21_1",
"attribute_1": "21_1"
},{
"id": "a21_2",
"attribute_2": "21_2"
}]
}]
}
select * from json_test where
array1.[id='a12'].array2.attribute_1='21_1' and
array1.[id='a12'].array2.attribute_2='21_2'