我需要帮助来清理这是我所拥有的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"}]}]
答案 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