从postgresql jsonb表中提取特定的json键/值

时间:2017-01-25 12:04:18

标签: json postgresql

我正在使用postgres 9.5,我有一个这样的表:

create table t1 (
 id serial,
 fkid int not null,
 tstamp timestamp with time zone,
 data jsonb
)

典型的json是:

{
    "WifiStatistic": {
        "Interfaces": {
            "wlan0": {
                "qualityLevel": {
                    "type": "graph",
                    "unit": "string",
                    "value": "75",
                    "graphDisplayName": "Quality level"
                },
                "SNR": {
                    "type": "graph",
                    "unit": "string",
                    "value": "75",
                    "graphDisplayName": "SNR"}
            }
        }
    }
}

提取质量等级的查询结果如下所示:

id | fkid | tstamp     | value | graphdisplayName
-------------------------------------------------
1  |  1   | 2017-01-22 |   75  |  "Quality Level"

我可以使用哪种查询?

1 个答案:

答案 0 :(得分:0)

感谢@VaoTsun的评论,我最终使用了这个:

select 
 tstamp, id, fkid, 
 data->'WifiStatistics'->'Interfaces'->'wlan0'->'qualityLevel'->'value' as value,
 data #>'{WifiStatistics, Interfaces, wlan0, qualityLevel}' ->'graphDisplayName' as dname
 from data;

我试图用一个json选择恢复两个值,这就是困惑我的。