例如,假设我的变量列“ xyz”包含如下数据:
{
"post_new_visits": "Repeat",
"post_new1_week": "Thursday",
"post_new2_appt": "Weekday",
"post_new3_site": "12:50AM",
"post_new4_channel": "5.0",
"pre_new2_appt": "Weekday",
"pre_new3_site": "12:50AM",
"pre_new4_channel": "5.0"
}
我想从上方的变体列中新建一个变体列,该变体列应仅具有“ post *”键值,如下所示,输出应如下所示。
{
"post_new_visits": "Repeat",
"post_new1_week": "Thursday",
"post_new2_appt": "Weekday",
"post_new3_site": "12:50AM",
"post_new4_channel": "5.0",
}
我有什么办法可以做到这一点?
答案 0 :(得分:0)
也许您可以展平并重建JSON。例如:
create table tmp ( v variant )
as
select
parse_json(
'{ "post_new_visits": "Repeat",
"post_new1_week": "Thursday",
"post_new2_appt": "Weekday",
"post_new3_site": "12:50AM",
"post_new4_channel": "5.0",
"pre_new2_appt": "Weekday",
"pre_new3_site": "12:50AM",
"pre_new4_channel": "5.0"}'
)
union all
select
parse_json(
'{ "post_new_visits": "New",
"post_new1_week": "Friday",
"post_new2_appt": "Weekday",
"post_new3_site": "13:50AM",
"post_new4_channel": "4.0",
"pre_new2_appt": "Weekday",
"pre_new3_site": "14:50AM",
"pre_new4_channel": "2.0"}'
);
select
OBJECT_AGG(v2.key, v2.value)
from
tmp,
lateral flatten(input => v) v2
where
v2.key like 'post%'
group by
v2.seq;