如何提取在 BigQuery 中存储为字符串的 JSON 数组
Posted
技术标签:
【中文标题】如何提取在 BigQuery 中存储为字符串的 JSON 数组【英文标题】:How to extract JSON array stored as string in BigQuery 【发布时间】:2020-10-02 06:46:11 【问题描述】:我有一个与此类似的 JSON 数组
["key":"Email","slug":"customer-email","value":"abc@gmail.com","key":"Phone Number","slug":"mobile-phone-number","value":"123456789","key":"First Name","slug":"first-name","value":"abc","key":"Last Name","slug":"last-name","value":"xyz","key":"Date of birth","slug":"date-of-birth","value":"01/01/1990"]
但棘手的部分是,这个数组存储为字符串。所以我认为第一步是将字符串转换为数组,然后将其取消嵌套,然后按照here中的方法进行操作@
我想知道这种方法是否可行,如果可行,我猜我面临的挑战是将字符串转换为数组。如果没有,或者您有更有效的方法,请提供帮助。谢谢
【问题讨论】:
【参考方案1】:你试过json_extract_array
select json_extract_array(
"""["key":"Email","slug":"customer-email","value":"abc@gmail.com","key":"Phone Number","slug":"mobile-phone-number","value":"123456789","key":"First Name","slug":"first- name","value":"abc","key":"Last Name","slug":"last-name","value":"xyz","key":"Date of birth","slug":"date-of-birth","value":"01/01/1990"]""");
【讨论】:
太神了,救了我的命【参考方案2】:以下是 BigQuery 标准 SQL
#standardSQL
SELECT
id,
JSON_EXTRACT_ARRAY(json_string) AS json_array
FROM `project.dataset.table`
如果适用于您的问题中的示例数据,如下所示
#standardSQL
WITH `project.dataset.table` AS (
SELECT 1 id, '["key":"Email","slug":"customer-email","value":"abc@gmail.com","key":"Phone Number","slug":"mobile-phone-number","value":"123456789","key":"First Name","slug":"first-name","value":"abc","key":"Last Name","slug":"last-name","value":"xyz","key":"Date of birth","slug":"date-of-birth","value":"01/01/1990"]' json_string
)
SELECT
id,
JSON_EXTRACT_ARRAY(json_string) AS json_array
FROM `project.dataset.table`
输出是
Row id json_array
1 1 "key":"Email","slug":"customer-email","value":"abc@gmail.com"
"key":"Phone Number","slug":"mobile-phone-number","value":"123456789"
"key":"First Name","slug":"first-name","value":"abc"
"key":"Last Name","slug":"last-name","value":"xyz"
"key":"Date of birth","slug":"date-of-birth","value":"01/01/1990"
从这一点开始 - 您可以使用您在问题中引用的 How do I parse value from JSON array into columns in BigQuery 中的解决方案
【讨论】:
我将相同的 JSON 数组作为 STRING 存储在我的一列中。我需要提取“值”的值,即仅 email_id 作为查询整个列的结果。我如何在单选查询中做到这一点?即字符串是 '["key":"Email","slug":"customer-email","value":"abc@gmail.com"' 我如何提取 'abc@gmail.com' 作为查询结果。以上是关于如何提取在 BigQuery 中存储为字符串的 JSON 数组的主要内容,如果未能解决你的问题,请参考以下文章
如何在存储为字符串的 bigquery 字段中取消嵌套多个数组?