Jih*_*hoi 3 google-bigquery bigquery-udf
我想用动态键提取嵌套的 JSON。我目前可以借助和提取密钥,但解析 JSON 对象的值会产生意外的结果。“[对象对象]”12
使用动态键和值解析嵌套 JSON 的正确方法是什么?(我不想使用自定义 JS UDF,但我不确定现有的 JSON 函数是否可以处理该问题。)
{
"key1":{"ItemID":1,"UseCount":4,"ItemCount":7},
"key2":{"ItemID":2,"UseCount":5,"ItemCount":8},
"key3":{"ItemID":3,"UseCount":6,"ItemCount":9}
...
}
Run Code Online (Sandbox Code Playgroud)
bigquery-utils:
json_extract_keys()
json_extract_values()
WITH
sample_logs AS (
SELECT '{"key1":{"ItemID":1,"UseCount":4,"ItemCount":7},"key2":{"ItemID":2,"UseCount":5,"ItemCount":8},"key3":{"ItemID":3,"UseCount":6,"ItemCount":9}}' as json_string,
UNION ALL SELECT '{"key4":{"ItemID":1,"UseCount":4,"ItemCount":7},"key5":{"ItemID":2,"UseCount":5,"ItemCount":8}}'
)
SELECT
json_string,
key,
TO_JSON_STRING(value) as value,
FROM sample_logs
CROSS JOIN UNNEST(bqutil.fn.json_extract_keys(json_string)) as key WITH OFFSET
INNER JOIN UNNEST(bqutil.fn.json_extract_values(json_string)) as value WITH OFFSET USING (OFFSET)
;
Run Code Online (Sandbox Code Playgroud)
JSON_STRING1 | "key1" | {"ItemID":1,"UseCount":4,"ItemCount":7} -- <- not [object Object]
JSON_STRING1 | "key2" | {"ItemID":2,"UseCount":5,"ItemCount":8}
JSON_STRING1 | "key3" | {"ItemID":3,"UseCount":6,"ItemCount":9}
JSON_STRING2 | "key4" | {"ItemID":1,"UseCount":4,"ItemCount":7}
JSON_STRING2 | "key5" | {"ItemID":2,"UseCount":5,"ItemCount":8}
Run Code Online (Sandbox Code Playgroud)
你可以考虑下面。
WITH
sample_logs AS (
SELECT '{"key1":{"ItemID":1,"UseCount":4,"ItemCount":7},"key2":{"ItemID":2,"UseCount":5,"ItemCount":8},"key3":{"ItemID":3,"UseCount":6,"ItemCount":9}}' as json_string,
UNION ALL SELECT '{"key4":{"ItemID":1,"UseCount":4,"ItemCount":7},"key5":{"ItemID":2,"UseCount":5,"ItemCount":8}}'
)
SELECT json_string,
key,
PARSE_JSON(json_string)[key] value
FROM sample_logs,
UNNEST(bqutil.fn.json_extract_keys(json_string)) as key;
Run Code Online (Sandbox Code Playgroud)
查询结果