如何展平 Json 具有大量子值的数据
How to flatten Json data which having high number of child values
我需要将 json 格式的数据扁平化为结构化的 table 格式。 json 这种格式的数据。它有100多个国家。我尝试了几种方法,但找不到解决方案。
{
"Country":{
"0":"USA",
"1":"Japan",
"2":"Canada",
"3":"Mexico"
},
"Capital":{
"0":"Washington",
"1":"Tokyo",
"2":"Ottawa",
"3":"Mexico City"
},
"Currency":{
"0":"USD",
"1":"Yen",
"2":"Canadian Dollar",
"3":"Peso"
}
}
到,
Id| country | Capital | Currency|
-----------------------------------
0| USA | Washington | USD |
1| Japan | Tokyo | Yen |
等等
要在同时遍历 3 个数组时展平,请确保在取消嵌套时匹配每个 lateral flatten()
键:
with data as (select parse_json('{
"Country":{
"0":"USA",
"1":"Japan",
"2":"Canada",
"3":"Mexico"
},
"Capital":{
"0":"Washington",
"1":"Tokyo",
"2":"Ottawa",
"3":"Mexico City"
},
"Currency":{
"0":"USD",
"1":"Yen",
"2":"Canadian Dollar",
"3":"Peso"
}
}') v)
select country.value::varchar country
, capital.value::varchar capital
, currency.value::varchar currency
from data
, lateral flatten(input => v:Country) country
, lateral flatten(input => v:Capital) capital
, lateral flatten(input => v:Currency) currency
where country.key=capital.key
and country.key=currency.key
我需要将 json 格式的数据扁平化为结构化的 table 格式。 json 这种格式的数据。它有100多个国家。我尝试了几种方法,但找不到解决方案。
{
"Country":{
"0":"USA",
"1":"Japan",
"2":"Canada",
"3":"Mexico"
},
"Capital":{
"0":"Washington",
"1":"Tokyo",
"2":"Ottawa",
"3":"Mexico City"
},
"Currency":{
"0":"USD",
"1":"Yen",
"2":"Canadian Dollar",
"3":"Peso"
}
}
到,
Id| country | Capital | Currency|
-----------------------------------
0| USA | Washington | USD |
1| Japan | Tokyo | Yen |
等等
要在同时遍历 3 个数组时展平,请确保在取消嵌套时匹配每个 lateral flatten()
键:
with data as (select parse_json('{
"Country":{
"0":"USA",
"1":"Japan",
"2":"Canada",
"3":"Mexico"
},
"Capital":{
"0":"Washington",
"1":"Tokyo",
"2":"Ottawa",
"3":"Mexico City"
},
"Currency":{
"0":"USD",
"1":"Yen",
"2":"Canadian Dollar",
"3":"Peso"
}
}') v)
select country.value::varchar country
, capital.value::varchar capital
, currency.value::varchar currency
from data
, lateral flatten(input => v:Country) country
, lateral flatten(input => v:Capital) capital
, lateral flatten(input => v:Currency) currency
where country.key=capital.key
and country.key=currency.key