【问题标题】:Redshift Postgresql - How to Parse Nested JSONRedshift Postgresql - 如何解析嵌套的 JSON
【发布时间】:2020-02-10 22:47:20
【问题描述】:

我正在尝试使用 JSON_EXTRACT_PATH_TEXT() 函数解析 JSON 文本。 JSON 示例:

{ 
   "data":[ 
      { 
         "name":"ping",
         "idx":0,
         "cnt":27,
         "min":16,
         "max":33,
         "avg":24.67,
         "dev":5.05
      },
      { 
         "name":"late",
         "idx":0,
         "cnt":27,
         "min":8,
         "max":17,
         "avg":12.59,
         "dev":2.63
      }
   ]
}
'

我尝试了 JSON_EXTRACT_PATH_TEXT(event , '{"name":"late"}', 'avg') 函数来获取 name = "late" 的 'avg',但它返回空白。 有人可以帮忙吗? 谢谢

【问题讨论】:

    标签: sql arrays json postgresql amazon-redshift


    【解决方案1】:

    在 Redshift 中这是一项相当复杂的任务,与 Postgres 不同的是,它具有 poor support to manage JSON,并且没有取消嵌套数组的功能。

    这是使用数字表的一种方法;您需要使用从0 开始的递增数字填充表格,例如:

    create table nums as
        select 0 i union all select 1 union all select 2 union all select 3 
        union all select 4 union all select 5 n union all select 6 
        union all select 7 union all select 8 union all select 9
    ;
    

    创建表后,您可以使用它使用json_extract_array_element_text() 遍历JSON 数组,并使用json_extract_path_text() 检查其内容:

    select json_extract_path_text(item, 'avg') as my_avg
    from (
        select json_extract_array_element_text(t.items, n.i, true) as item
        from (
            select json_extract_path_text(mycol, 'data', true ) as items
            from mytable
        ) t
        inner join nums n on n.i < json_array_length(t.items, true)
    ) t
    where json_extract_path_text(item, 'name') = 'late';
    

    【讨论】:

      【解决方案2】:

      您需要为此使用json_array_elements

      select obj->'avg'
        from foo f, json_array_elements(f.event->'data') obj 
      where obj->>'name' = 'late';
      

      Working example

      create table foo (id int, event json);
      insert into foo values (1,'{ 
         "data":[ 
            { 
               "name":"ping",
               "idx":0,
               "cnt":27,
               "min":16,
               "max":33,
               "avg":24.67,
               "dev":5.05
            },
            { 
               "name":"late",
               "idx":0,
               "cnt":27,
               "min":8,
               "max":17,
               "avg":12.59,
               "dev":2.63
            }]}');
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-06-02
        • 2021-02-25
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-05-08
        相关资源
        最近更新 更多