Query the element in Array inside JSON column

I have a jason column in my postgress sql Database. I have several properties in that jason column. I can search properties using below query.

SELECT *  FROM public.object_reference where value->>'name' = 'Sam' and value->>'address' ='home';

But my problem is I have a Array inside that JSON column. That Array has key and value pair. Below is the sample of that array

"attributes": [ {
    "value": "Sam",
    "key": "name"
}, {
    "value": "abc",
    "key": "address"
}, {
    "value": "Singapore",
    "key": "country"
}, {
    "value": "97813245",
    "key": "mobile"
}, {
    "value": "Engineer",
    "key": "position"
},
"id": "1312312",
"type": "Job",
"classid": "1245568956643546788907634"

}

So i need to get the value of name in the attributes array (inside JSON column). This is json type column, not a jsonb type.

Upvotes: 0

Views: 359

Answers (1)

Emilio Platzer
Emilio Platzer

Reputation: 2469

You can deconstruct the array inside de object transforming it in a set of recordet (pseudo table) with json_array_elements:

select pair->>'value'
  from has_json,json_array_elements(obj->'attributes') as pair
  where pair->>'key' = 'name';

You can see a running example at: http://rextester.com/ONJZ8486

Upvotes: 1

Related Questions