我需要有关分离 JSON 键和值对的帮助。我花了几个小时的谷歌搜索,但还没有真正找到一种方法来做到这一点。我希望在这里找到一些答案。
所以,我这里有一个数据集,由
department
(string) 和 assets
(stringified JSON) 组成。我能够将 assets
解析为 JSON,接下来我要做的就是分离它的键和值(我在下面说明)。
我尝试过 UNNEST 但它似乎不支持 JSON 值。
我怎样才能实现这个目标?
雅典娜查询:
WITH dataset AS
(SELECT 'engineering' AS department, '{"number_of_assets": {"computer": "95"}}' AS assets )
SELECT *
FROM
(SELECT department,
json_extract(assets,
'$.number_of_assets') AS number_of_assets
FROM dataset)
您可以将提取的 json 转换为
MAP(VARCHAR, VARCHAR)
(或 MAP(VARCHAR, INTEGER)
)并取消嵌套结果:
WITH dataset AS (
SELECT 'engineering' AS department,
'{"number_of_assets": {"computer": "95"}}' AS assets
)
SELECT department, asset, count
FROM (
SELECT department,
cast(json_extract(assets, '$.number_of_assets') as MAP(VARCHAR, VARCHAR)) AS number_of_assets
FROM dataset
) t
CROSS JOIN UNNEST(number_of_assets) AS t (asset, count);
输出:
部门 | 资产 | 数 |
---|---|---|
工程 | 电脑 | 95 |