I have a table that looks like this:
And the JSON has dynamic keys and looks like this:
{
key_1:{
value_1:a
value_2:b
},
key_2:{
value_1:c
value_2:d
}
}
I need to parse this table to get an output that looks like this:
Tried it with JS functions but couldn't get it quite right. Thanks in advance: )
Consider below approach
create temp function get_keys(input string) returns array<string> language js as """
return Object.keys(JSON.parse(input));
""";
create temp function get_values(input string) returns array<string> language js as """
return Object.values(JSON.parse(input));
""";
create temp function get_leaves(input string) returns string language js as '''
function flattenObj(obj, parent = '', res = {}){
for(let key in obj){
let propName = parent ? parent + '.' + key : key;
if(typeof obj[key] == 'object'){
flattenObj(obj[key], propName, res);
} else {
res[propName] = obj[key];
}
}
return JSON.stringify(res);
}
return flattenObj(JSON.parse(input));
''';
create temp table temp as (
select format('%t', t) row_id, date, name, val,
split(key, '.')[offset(0)] as key,
split(key, '.')[offset(1)] as col,
from your_table t, unnest([struct(get_leaves(json_extract(json, '$')) as leaves)]),
unnest(get_keys(leaves)) key with offset
join unnest(get_values(leaves)) val with offset using(offset)
);
execute immediate (
select '''
select * except(row_id) from temp
pivot (any_value(val) for col in ("''' || string_agg(distinct col, '","') || '"))'
from temp
);
if applied to sample data in your question - output is
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.