【发布时间】:2021-11-30 15:42:51
【问题描述】:
我已经搜索了 Stack Overflow 以找到我的问题的答案,但是虽然我发现了许多有趣的案例,但没有一个能完全解决我的问题。
我的数据中有一个名为 fields 的列,其中包含 JSON 信息,如下所示:
Row Fields
1 [{"label":"Label 1","key":"label_1","description":"Value of label_1"},{"label":"Label 2","key":"label_2","error":"Something"}]
2 [{"description":"something","label":"Row 1","key":"row_1"},{"label":"Row 2","message":"message_1","key":"row_2"}]
本质上,我有很多 JSON 行,其中包含 label 和 key,以及其他类似的参数。从每个 {} 中,我只想提取 label 和 key,然后(可选,但理想情况下)将每个 {} 中的每个 label 和 key 拉伸到自己的行。所以,结果,我会得到以下输出:
Row Label Key
1 Label 1 label_1
1 Label 2 label_2
2 Row 1 row_1
2 Row 2 row_2
请注意,label 和 key 在 JSON 中的内容可以是任何内容(字符串、整数、特殊字符、所有内容的混合等)。此外,key 和 label 可以是任何相关的到每个 {} 中的其他参数。
为方便起见,这里是 Big Query SQL 虚拟数据:
SELECT '1' AS Row, '[{"label":"Label 1","key":"label_1","description":"Value of label_1"},{"label":"Label 2","key":"label_2","error":"Something"}]' AS Fields
UNION ALL
SELECT '2' AS Row, '[{"description":"something","label":"Row 1","key":"row_1"},{"label":"Row 2","message":"message_1","key":"row_2"}]' AS Fields
我首先想到的是使用 REGEX 来隔离所有括号,并且只向我显示 label 和 key 的信息。然后,我查看了 JSON 函数的 BQ 文档,并且非常卡在 json_path 参数上,特别是因为他们的示例与我的不匹配。
【问题讨论】:
标签: arrays json google-bigquery