【问题标题】:Convert json dictionary column to key-value pair rows (Redshift+Postgresql)将 json 字典列转换为键值对行(Redshift+Postgresql)
【发布时间】:2016-02-23 18:59:27
【问题描述】:

我在 redshift 中有以下架构(一个 id 列和一个 json 字典作为字符串)

id data
1 {'1': 'true', '2':'false'}
2 {'1': 'false', '3':'true'}

我想将其转换为:

id key value
1 '1' 'true'
1 '2' 'false'
2 '1' 'false'
2 '3' 'true'

什么是合适的 postgresql 查询呢?我曾尝试使用 JSON_EXTRACT_PATH_TEXT,但随后我需要将所有单独的键合并在一起,并且我正在处理数百个键。

【问题讨论】:

    标签: json postgresql amazon-redshift


    【解决方案1】:

    类似

    select id,json_object_keys(t.data) as key,data->json_object_keys(t.data) as value from t;
    

    http://sqlfiddle.com/#!15/781fd/10

    【讨论】:

    • 不幸的是,redshift 使用 postgresql 8.1,因此来自更高版本的 postgresql 的 json 函数不起作用。以上给了我以下错误“[42883] ERROR: function json_object_keys("unknown") does not exist”
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多