I am currently experimenting with the JSON functionality PostgreSQL. While queries of nested key-value pair objects are very easy to query, I am having trouble querying JSON arrays. To be more specific, I have a table mytable
with two columns: An integer primary key id
and a JSONB column info
. The JSONB data have the following structure:
{
"modes": [
{
"params": [
{"name": "x", "value": 10},
{"name": "y", "value": 15}
]
},
{
"params": [
{"name": "x", "value": 20},
{"name": "y", "value": 25}
]
}
]
}
I want to select all table rows that have at least one params
element whose name
is x
and has a value between 15
and 25
. A pseudo-SQL query would be:
SELECT
*
FROM
mytable
WHERE
(info->'modes[*]'->'params[*]'->>'name')::TEXT = 'x'
AND
(info->'modes[*]'->'params[*]'->>'value')::FLOAT BETWEEN 15 AND 25;
I am using PostgreSQL 9.6.2