Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Query for element of array in JSON column

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:

{
   "id": "123",
   "name": "foo",
   "emails":[
      {
        "id": "123",
        "address": "somethinghere"
      },
      {
        "id": "456",
        "address": "soemthing"
      }
   ]
} 

This is just dummy data for the purpose of the question.

Is it possible to query for a specific item in the emails array based on the id?
Pretty much: "return email where id=123)"?

like image 870
Joe Avatar asked Oct 24 '13 13:10

Joe


People also ask

How do I query a JSON column in SQL?

To query JSON data, you can use standard T-SQL. If you must create a query or report on JSON data, you can easily convert JSON data to rows and columns by calling the OPENJSON rowset function. For more information, see Convert JSON Data to Rows and Columns with OPENJSON (SQL Server).

How do I query a JSON column in PostgreSQL?

Querying JSON dataPostgreSQL provides two native operators -> and ->> to help you query JSON data. The operator -> returns JSON object field by key. The operator ->> returns JSON object field by text.

Which function can be used to retrieve a JSON object from server?

In this article, we explored JSON_QUERY() function to retrieve the JSON object and array from JSON data. It is a useful function to work with SQL Server JSON data. You should explore these functions and be familiar with the Javascript object Notation.

What is the query with used for to see JSON format?

Use the FOR JSON clause to simplify client applications by delegating the formatting of JSON output from the app to SQL Server. Azure Data Studio is the recommended query editor for JSON queries because it auto-formats the JSON results (as seen in this article) instead of displaying a flat string.


3 Answers

For Postgres 9.4+ see adamc's later answer. Or:

  • Query for array elements inside JSON type

Original answer for Postgres 9.3

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 the name of the JSON column.

See also:

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

About the implicit CROSS JOIN LATERAL:

  • PostgreSQL unnest() with element number

Index to support this kind of query:

  • Index for finding an element in a JSON array
like image 132
Erwin Brandstetter Avatar answered Oct 13 '22 13:10

Erwin Brandstetter


With a JSONB column in Postgres 9.4+ you can use the contains operator @> to query for an element in an array:

SELECT * FROM jsontest WHERE data @> '{ "emails": [{ "id": "123" }] }';

See Query for array elements inside JSON type for more details.

Here is a working example:

CREATE TABLE jsontest(data JSONB NOT NULL);
INSERT INTO jsontest VALUES (
  '{
     "name": "foo",
     "id": "123",
     "emails": 
     [
       {
         "address": "somethinghere",
         "id": "123"
       },
       {
         "address": "soemthing",
         "id": "456"
       }
     ]
  }'
);
SELECT * FROM jsontest WHERE data @> '{ "emails": [{ "id": "123" }] }';

data
----
{"id": "123", "name": "foo", "emails": [{"id": "123", "address": "somethinghere"}, {"id": "456", "address": "soemthing"}]}

(1 row)

like image 37
adamc Avatar answered Oct 13 '22 13:10

adamc


Came across this post and found that you can directly query on table like this:

SELECT *
FROM   table_name, json_array_elements(json_column) AS data
WHERE  data->>'id' = 123;

Omitting this part:

json_array_elements(t.json_col->'emails')
like image 3
Deepak Mahakale Avatar answered Oct 13 '22 14:10

Deepak Mahakale