【发布时间】:2022-01-23 00:00:40
【问题描述】:
我有以下 JSON 字符串(我为可见性添加了换行符,在实际代码中,所有这些都被压缩在一行中)
{"schema":
{"properties":
{"key_1":{"label":"key 1","type":"string"},
"key_2":{"label":"key 2","type":"string"},
"ley_3":{"label":"key 3","type":"string"},
"key_4":{"label":"key 4","type":"string"},
...
}
}
}
我要做的是提取与该键关联的所有键和标签。我知道how to do this when key is explicitly stated in JSON,但在此示例中未明确说明密钥。
我关注Google Big Query documentation 处理 JSON 字符串,以下是我的进展:
SELECT json_schema, JSON_EXTRACT(json_schema, "$.schema.properties"), JSON_EXTRACT(json_schema, "$.schema.properties[1]")
FROM schemas
json_schema 是schemas 表中的列名。
这让我找到了正确的方向,但我不知道如何从这里开始。我想要的输出是(例如),是:
key value
key_1 key 1
key_2 key 2
key_3 key 3
key_4 key 4
这里是重现示例的代码:
SELECT '{"schema":{"properties":{"key_1":{"label":"key 1","type":"string"},"key_2":{"label":"key 2","type":"string"},"key_3":{"label":"key 3","type":"string"},"key 4":{"label":"key_4","type":"string"}}}}' AS json_schema
【问题讨论】:
标签: sql json google-bigquery