如何展平具有大量子值的 Json 数据
Posted
技术标签:
【中文标题】如何展平具有大量子值的 Json 数据【英文标题】:How to flatten Json data which having high number of child values 【发布时间】:2021-02-26 09:02:20 【问题描述】:我需要将 json 格式的数据扁平化为结构化的表格格式。这种格式的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 |
等等
【问题讨论】:
【参考方案1】:要在同时迭代 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 数据的主要内容,如果未能解决你的问题,请参考以下文章
从 Firebase 查询具有在 ArrayList 中找到的子值的对象