Query for element of array in JSON column

后端 未结 4 1317
忘了有多久
忘了有多久 2020-11-27 16:32

Recently upgraded to using PostgreSQL 9.3.1 to leverage the JSONfunctionalities. In my table I have a json type column that has a structure like this:

{
   \         


        
4条回答
  •  抹茶落季
    2020-11-27 17:06

    Yes, that's possible:

    SELECT *
    FROM   tbl t, json_array_elements(t.json_col->'emails') AS elem
    WHERE  elem->>'id' = 123;
    

    tbl being your table name, json_col being the name of the JSON column.

    More details in this related answer:

    • How do I query using fields inside the new PostgreSQL JSON datatype?

    More about the implicit CROSS JOIN LATERAL in the last paragraph of this related answer:

    • PostgreSQL unnest() with element number

    Index to support this kind of query:

    • Index for finding an element in a JSON array

提交回复
热议问题