如何在 Amazon Athena 中分离 JSON 键值对?

How to separate JSON key and value pair in Amazon Athena?

我需要有关分离 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);

输出:

department asset count
engineering computer 95