BigQuery concat 嵌套数组 json
Posted
技术标签:
【中文标题】BigQuery concat 嵌套数组 json【英文标题】:BigQuery concat nested array json 【发布时间】:2020-11-26 05:43:07 【问题描述】:我的数据看起来像
"Attributes": [
"values": [
"value": "20003"
,
"value": "30075"
,
"value": "40060"
],
"name": "price"
],
"attr2" : "val"
我想要的输出是连接嵌套 json 数组中的所有值
price, "20003, 30075, 40060"
我尝试了一些查询,但未能获得正确的输出。
【问题讨论】:
【参考方案1】:您可以使用JSON_EXTRACT_ARRAY 和ARRAY_TO_STRING:
WITH test_json AS (
SELECT
'''
"Attributes": [
"values": [
"value": "20003"
,
"value": "30075"
,
"value": "40060"
],
"name": "price"
],
"attr2" : "val"
''' AS json_string
),
values_concatenated AS (
SELECT ARRAY_TO_STRING(
ARRAY(
SELECT JSON_VALUE(json_values, '$.value')
FROM UNNEST((SELECT JSON_EXTRACT_ARRAY(json_string, '$.Attributes[0].values') AS json_values FROM test_json)) as json_values
),
', '
) as values
)
SELECT
(select json_value(json_string, '$.Attributes[0].name') from test_json),
(select values from values_concatenated)
【讨论】:
以上是关于BigQuery concat 嵌套数组 json的主要内容,如果未能解决你的问题,请参考以下文章