在 Google BigQuery 中读取多级重复 JSON 结构数据

Reading Multiple levels of repeated JSON structure data in Google BigQuery

我正在尝试阅读下面的 JSON 结构。能够将数据提升到 1 级并正确查看其读数,但是当我深入到另一个 sections.its 时,无法正确读取数据。以下是供您细读的输入数据和我正在使用的代码以及输出。请求你的帮助。

代码:

CREATE TEMPORARY FUNCTION CUSTOM_JSON_EXTRACT(json STRING, json_path STRING)
RETURNS ARRAY<STRING>
LANGUAGE js AS """
  var result = jsonPath(JSON.parse(json), json_path);
  if(result){return result;} 
  else {return [];}
"""
OPTIONS (
    library="gs://temp-dev-workspace/json_temp/jsonpath-0.8.0.js"
);

SELECT job_id,
combo_id,
tool_code
from temp.dbm_eqp_data,
UNNEST(CUSTOM_JSON_EXTRACT(conv_column, '$.Combos[*].ComboId')) combo_id,
UNNEST(CUSTOM_JSON_EXTRACT(conv_column, '$.Combos[*].Demands.DownHoleTools.PrimaryTools[*].ToolCode')) tool_code
;

预期输出:

以下适用于 BigQuery 标准 SQL

#standardSQL
CREATE TEMPORARY FUNCTION CUSTOM_JSON_EXTRACT(json STRING, json_path STRING)
RETURNS ARRAY<STRING>
LANGUAGE js AS '''
  var result = jsonPath(JSON.parse(json), json_path);
  if(result){return result;} 
  else {return [];}
'''
OPTIONS (
    library="gs://temp-dev-workspace/json_temp/jsonpath-0.8.0.js"
);
SELECT 
  job_id,
  combo_id,
  tool_code
FROM `temp.dbm_eqp_data`,
UNNEST(CUSTOM_JSON_EXTRACT(conv_column, '$.Combos[*].ComboId')) combo_id
LEFT JOIN UNNEST(CUSTOM_JSON_EXTRACT(conv_column, '$.Combos[?(@.ComboId=="' || combo_id || '")].Demands.DownHoleTools.PrimaryTools[*].ToolCode')) tool_code

如果应用于您问题中的示例数据 - 输出为

Row job_id  combo_id    tool_code    
1   1       9146        19139    
2   1       9146        7030     
3   1       9146        3707     
4   1       9146        3707     
5   1       5970        null     
6   1       5971        null     
7   1       26793       null