简体   繁体   English

查询JSON列中的Array中的元素

[英]Query the element in Array inside JSON column

I have a jason column in my postgress sql Database. 我在Postgress sql数据库中有一个jason列。 I have several properties in that jason column. 我在该jason列中有几个属性。 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. 但是我的问题是我在JSON列中有一个数组。 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). 所以我需要在属性数组(在JSON列内)中获取名称的值。 This is json type column, not a jsonb type. 这是json类型列,而不是jsonb类型。

You can deconstruct the array inside de object transforming it in a set of recordet (pseudo table) with json_array_elements : 您可以使用json_array_elements在de对象内部解构数组,将其转换为一组记录集(伪表):

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 您可以在以下位置查看正在运行的示例: http : //rextester.com/ONJZ8486

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM