简体   繁体   English

从JSON数据类型Postgres的数组中获取一些元素

[英]Get some elements from array on json data type postgres

I have a table "products" which have multi columns, on of these column "specifications" is json and I have this data on it: 我有一个表“ products”,其中有多列,在这些列中,“ specifications”是json,上面有这些数据:

[
        {
            "en":"Weight",
            "ar":"الوزن",
            "value":"100"
        },
        {
            "en":"Height",
            "ar":"الارتفاع",
            "value":"300"
        }
    ]

I need to get only "en, value" or "ar, value" from this field in addition to remain fields of the table. 除了保留表的其余字段外,我仅需要从此字段中获取“ en,value”或“ ar,value”。

Lets try this 让我们尝试一下

select column_names,
 --all column names you can left "specifications" column
(select string_agg(v,',') from
(select '(' || (val->>'en')::text || ',' || (val->>'value')::text || ')' as v 
    --You can put whatever json key-value you want here 
 from (select value as val from
(select value from json_array_elements("specifications"))))) from "products"

hope it works for you. 希望对你有效。

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

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