如何将 JSON 数组中的值解析为 BigQuery 中的列

Posted

技术标签:

【中文标题】如何将 JSON 数组中的值解析为 BigQuery 中的列【英文标题】:How do I parse value from JSON array into columns in BigQuery 【发布时间】:2020-09-04 03:28:34 【问题描述】:

我有一个类似的 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"

我希望将数组变成这样的列

email|          phoneNumber |  firstName |  lastName |  dob
abc@gmail.com   123456789      abc          xyz         01/01/1990

任何指南或意见将不胜感激。

【问题讨论】:

它在 BigQuery 表中吗?如果是这样 - 这是一个重复的字符串还是实际上是一个代表 json 数组的字符串?请澄清 对不起,不够清晰。是的,这是在 BigQuery 中,这是一个重复的字符串。 【参考方案1】:

以下是 BigQuery 标准 SQL

#standardSQL
SELECT id, 
  MAX(IF(key = 'Email', value, NULL)) AS Email,
  MAX(IF(key = 'PhoneNumber', value, NULL)) AS PhoneNumber,
  MAX(IF(key = 'FirstName', value, NULL)) AS FirstName,
  MAX(IF(key = 'LastName', value, NULL)) AS LastName,
  MAX(IF(key = 'Dateofbirth', value, NULL)) AS Dateofbirth
FROM `project.dataset.table`,
UNNEST(ARRAY(
    SELECT AS STRUCT 
      REPLACE(JSON_EXTRACT_SCALAR(json, '$.key'), ' ', '') AS key,
      JSON_EXTRACT_SCALAR(json, '$.value') AS value
    FROM UNNEST(json_array) json
))
GROUP BY id   

您可以使用您问题中的示例数据进行测试,如以下示例所示

#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_array
)
SELECT id, 
  MAX(IF(key = 'Email', value, NULL)) AS Email,
  MAX(IF(key = 'PhoneNumber', value, NULL)) AS PhoneNumber,
  MAX(IF(key = 'FirstName', value, NULL)) AS FirstName,
  MAX(IF(key = 'LastName', value, NULL)) AS LastName,
  MAX(IF(key = 'Dateofbirth', value, NULL)) AS Dateofbirth
FROM `project.dataset.table`,
UNNEST(ARRAY(
    SELECT AS STRUCT 
      REPLACE(JSON_EXTRACT_SCALAR(json, '$.key'), ' ', '') AS key,
      JSON_EXTRACT_SCALAR(json, '$.value') AS value
    FROM UNNEST(json_array) json
))
GROUP BY id   

有输出

Row id  Email           PhoneNumber     FirstName   LastName    Dateofbirth  
1   1   abc@gmail.com   123456789       abc         xyz         01/01/1990  

【讨论】:

以上是关于如何将 JSON 数组中的值解析为 BigQuery 中的列的主要内容,如果未能解决你的问题,请参考以下文章

如何将 alamofire 返回 json 解析为 Swift 中的字符串数组?

如何将bash脚本中的json解析为数组?数组值应该同时具有 key:value 格式

如何将 JSON 数组解析为字符串? [复制]

在我们将其解析为 JSON 之前,Snowflake 如何转义对象数组字符串中的所有特殊字符?

如何解析JSON.NET中的匿名数组?

无法将 JSON 解析为 UITableView