galiolio
galiolio

Reputation: 275

Check a value in an array inside a object json in PostgreSQL 9.5

I have an json object containing an array and others properties.

I need to check the first value of the array for each line of my table.

Here is an example of the json

{"objectID2":342,"objectID1":46,"objectType":["Demand","Entity"]}

So I need for example to get all lines with ObjectType[0] = 'Demand' and objectId1 = 46.

This the the table colums

id |  relationName | content

Content column contains the json.

Upvotes: 0

Views: 54

Answers (1)

Vao Tsun
Vao Tsun

Reputation: 51496

just query them? like:

t=# with table_name(id, rn, content) as (values(1,null,'{"objectID2":342,"objectID1":46,"objectType":["Demand","Entity"]}'::json))
select * From table_name
where content->'objectType'->>0 = 'Demand' and content->>'objectID1' = '46';
 id | rn |                              content
----+----+-------------------------------------------------------------------
  1 |    | {"objectID2":342,"objectID1":46,"objectType":["Demand","Entity"]}
(1 row)

Upvotes: 1

Related Questions