简体   繁体   English

如何从 BigQuery 中的列解析 JSON 文件

[英]How to parse JSON file from a column in BigQuery

This is the JSON file that I am trying to parse这是我试图解析的 JSON 文件

[{"message":"Value","code":"1234"}]

I am trying to parse the message from the JSON I think the challenge is because the JSON started with "[" instead of the more common "{"我正在尝试解析来自 JSON 的消息我认为挑战是因为 JSON 以“[”而不是更常见的“{”开头

This is the query that I thought was right but apparently it's not这是我认为正确的查询,但显然不是

JSON_EXTRACT_SCALAR(response,'$[0].message')

Please note that "response" is the name of the column aka the json_string_expr请注意,“响应”是列的名称,也就是 json_string_expr

Might be a typo in your real code/query?可能是您的真实代码/查询中的错字?
Works for me as is - see example below按原样对我有用 - 请参见下面的示例

#standardSQL
WITH `project.dataset.table` AS (
  SELECT '[{"message":"Value","code":"1234"}]' response
)
SELECT *, JSON_EXTRACT_SCALAR(response,'$[0].message')
FROM `project.dataset.table`

with output带输出

Row response                            f0_  
1   [{"message":"Value","code":"1234"}] Value   

You can apply:您可以申请:

WITH `project.dataset.table` AS (
  SELECT '{\n \"type\":\"invalid\",\n \"code\":\"invalidRequest\",\n \"details\":\"Missing or invalid Parameters\",\n \"moreInfo\":{\n \"fieldLevelErrors\":[\n {\n \"fieldName\":\"/*/prospect/identificationDocumentDetails[idType!=\\\"\\\"]/idNumber\",\n \"reasonCode\":\"Required/Invalid Format\",\n \"errorMessage\":\"Value\"\n }\n ]\n }\n}' response
)
SELECT JSON_EXTRACT(response,'$.details') as details, JSON_EXTRACT(response,'$.moreInfo.fieldLevelErrors[0].errorMessage') as error
FROM `project.dataset.table`

It returns the following result:它返回以下结果:

Row details                           error 
1   "Missing or invalid Parameters" "Value"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM