Quering JSON Datatype Fields in PostgreSQL
PostgreSQL 9.2 introduced the JSON datatype, but advanced querying of fields within JSON records required custom functions. PostgreSQL 9.3 expanded these capabilities with operators and functions.
Specifically, for a table with a JSON column named "data":
Postgres 9.3:
SELECT object FROM json_tbl , json_array_elements(data) AS object WHERE object->>'name' = 'Toby';
Postgres 9.4:
Postgres 9.5:
Postgres 12:
SELECT jsonb_path_query_first(data, '$[*] ? (@.name == "Toby")') AS object FROM jsonb_tbl WHERE data @> '[{ "name": "Toby"}]'; -- optional for indexing
Or equivalent:
... WHERE data @@ '$[*].name == "Toby"';
Additional resources:
The above is the detailed content of How to Query JSON and JSONB Data in PostgreSQL?. For more information, please follow other related articles on the PHP Chinese website!