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)"?
With a JSONB column in Postgres 9.4+ you can use the contains operator
@>
to query for an element in an array:See Query for array elements inside JSON type for more details.
Here is a working example:
(1 row)
Yes, that's possible:
tbl
being your table name,json_col
being the name of the JSON column.More details in this related answer:
More about the implicit
CROSS JOIN LATERAL
in the last paragraph of this related answer:Index to support this kind of query:
Came across this post and found that you can directly query on table like this:
Omitting this part:
You can do it as simple as :
it seems you store the id as string, if it was an integer you can do it like this :
or you can get all the rows with id > 10