I have the following table:
CREATE TABLE trip
(
id SERIAL PRIMARY KEY ,
gps_data_json jsonb NOT NULL
);
The JSON in gps_data_json conta
The problem arises because ->>
operator cannot walk through array:
json_array_elements
function;Following query does the trick:
WITH
A AS (
SELECT
Id
,jsonb_array_elements(gps_data_json) AS point
FROM trip
)
SELECT *
FROM A
WHERE (point->>'mode') = 'WALK';
Unnesting the array works fine, if you only want the objects containing the values queried. The following checks for containment and returns the full JSONB:
SELECT * FROM trip
WHERE gps_data_json @> '[{"mode": "WALK"}]';
See also Postgresql query array of objects in JSONB field