清理嵌套的JSON列

时间:2019-06-24 18:14:25

标签: sql amazon-redshift

我需要帮助来清理这是我所拥有的JSON列

select json_extract_path_text(response_text,'rule') from live.qamatha_responses limit 20

这是我要清除的列:

[{"ruleSet":"ccc","rule":"ERROR MESSAGE -- 3.2.1","business-identity-match-failed":"Businessname","approved":true,"requirements":[{"key":"business-match-failed","value":"Businessname okay","type":"text"}]}]

1 个答案:

答案 0 :(得分:0)

您面临的困难是需要将输入json数组加载到单独的行中。 Redshift没有内置对此的支持,因此您必须诉诸不太明显的解决方案。

input是一个包含要清除的json数组列的表。我复制了输入数组以包含2个json对象(在您的示例中,该数组只有一个对象)。

seq是一个帮助表,我们将使用它与input交叉连接。它应至少包含与json数组长度一样多的行。

exploaded_input是一个表,其中包含一行中数组中的一个json对象。

我希望现在一切都清楚了。

with input as (
    select '[{"ruleSet":"ccc","rule":"ERROR MESSAGE -- 3.2.1","business-identity-match-failed":"Businessname","approved":true,"requirements":[{"key":"business-match-failed","value":"Businessname okay","type":"text"}]}, {"ruleSet":"ccc","rule":"ERROR MESSAGE -- 3.2.1","business-identity-match-failed":"Businessname","approved":true,"requirements":[{"key":"business-match-failed","value":"Businessname okay","type":"text"}]}]'::text as response_text
), seq as (
    select row_number() over ()::int - 1 as i
    from (
             select 1 union all
             select 1 union all
             select 1 union all
             select 1 union all
             select 1
         ) as s
),
 exploaded_input as (
     select json_extract_array_element_text(response_text, seq.i) as response_text
     from input cross join seq
     where json_array_length(response_text) > seq.i
 )

select json_extract_path_text(response_text,'rule') as rule from exploaded_input

返回:

rule
ERROR MESSAGE -- 3.2.1
ERROR MESSAGE -- 3.2.1