【问题标题】:How do I flatten JSON data in Redshift?如何在 Redshift 中展平 JSON 数据?
【发布时间】:2022-11-01 21:13:21
【问题描述】:

我想将此 JSON 数据带入包含产品、价格和数量列的 Redshift 表中。我应该如何使用 Redshift 的 JSON 函数来做到这一点? https://docs.aws.amazon.com/redshift/latest/dg/json-functions.html

{ "lamp": { "price": 18.99, "quantity": 30 }, "desk": { "price": 129.99, "quantity": 12 }, "vase": { "price": 22.49, "quantity": 18 }, "speakers": { "price": 49.99, "quantity": 50 } }

我尝试过使用 JSON_EXTRACT_PATH_TEXT 函数,但我不确定如何将其应用于此数据(例如,它可以使用通配符来进行迭代吗?)。

【问题讨论】:

    标签: sql json amazon-redshift


    【解决方案1】:

    您可以使用 JSON_PARSE 代替 JSON_EXTRACT_PATH_TEXT,它返回可以轻松访问 json 对象的超级数据类型。 我不知道您从哪里获取数据,但这是我的示例。

    与您一起创建表格示例:

    create table test.temp_json
    (
        json_super super
    );
    insert into test.temp_json
    values (json_parse('{ "lamp": { "price": 18.99, "quantity": 30 }, "desk": { "price": 129.99, "quantity": 12 }, "vase": { "price": 22.49, "quantity": 18 }, "speakers": { "price": 49.99, "quantity": 50 } }'))
    
    

    访问数据:

    select key as product, value.price as price, value.quantity as quantity
    from test.temp_json j,
         UNPIVOT j.json_super AS value AT key;
    

    结果:

    • 灯, 18.99, 30
    • 办公桌, 129.99, 12
    • 花瓶, 22.49, 18
    • 扬声器, 49.99, 50

    阅读更多关于 json_parse 和 unpivot https://docs.aws.amazon.com/redshift/latest/dg/JSON_PARSE.html一个 https://docs.aws.amazon.com/redshift/latest/dg/query-super.html#unnest

    【讨论】:

      猜你喜欢
      • 2021-03-31
      • 1970-01-01
      • 2022-01-16
      • 2016-01-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-07-23
      • 1970-01-01
      相关资源
      最近更新 更多