简体   繁体   English

如何使用 JSON_EXTRACT 或 JSON_EXTRACT_SCALAR 在 Big Query 中读取多级 JSON 数据

[英]how to read multiple levels of JSON data in Big Query using JSON_EXTRACT or JSON_EXTRACT_SCALAR

I am trying to read the below JSON structure in Bigquery using JSON_EXTRACT in Bigquery..it says unsupported operator Path "*"我正在尝试使用 Bigquery 中的 JSON_EXTRACT 读取 Bigquery 中的以下 JSON 结构..它说不受支持的运算符路径“*”

Tried all the ways I can in BigQuery and Request your help在 BigQuery 中尝试了所有可能的方法并请求您的帮助

Error: Unsupported operator in JSONPath: *错误: JSONPath 中不支持的运算符:*

****JSON data:** JUST THE PORTION that has multiple values and which has Issues while reading. ****JSON 数据:** 只是具有多个值并且在阅读时有问题的部分。 Need to read all 4 "id" values below as an eg and need to read all other columns as well under Combo section which produces 4 rows with different ID,Type etc.**需要读取下面的所有 4 个“id”值作为一个例子,并且需要读取所有其他列以及组合部分下的产生 4 行不同 ID、类型等**

"Combos": [
  {
    "Id": "1111",
    "Type": 0,
    "Description": "ABCD",
    "ComboDuration": {
      "StartDate": "2009-10-26T08:00:00",
      "EndDate": "2009-10-29T08:00:00"
    }
  },
  {
    "Id": "2222",
    "Type": 1,
    "Description": "XYZ",
    "ComboDuration": {
      "StartDate": "2019-10-26T08:00:00",
      "EndDate": "2019-10-29T08:00:00"
    }
  },
  {
    "Id": "39933",
    "Type": 3,
    "Description": "General",
    "ComboDuration": {
      "StartDate": "2019-10-26T08:00:00",
      "EndDate": "2019-10-29T08:00:00"
    }
  },
  {
    "Id": "39934",
    "Type": 2,
    "Description": "ABCDXYZ",
    "ComboDuration": {
      "StartDate": "2019-10-26T08:00:00",
      "EndDate": "2019-10-29T08:00:00"
    }
  },

]

****Code:** P.S - conv_column is a string column where my JSON structure stored**

SELECT 
JSON_EXTRACT(conv_column,"$.Combos.*.Id") as combo_id
from lz.json_file

SELECT JSON_EXTRACT(conv_column,"$.Combos[*].Id") as combo_id
from lz.json_file

SELECT JSON_EXTRACT(conv_column,"$.Combos[?@.Id]") as combo_id
from lz.json_file

Below example BigQuery for Standard SQL下面是标准 SQL 的 BigQuery 示例

#standardSQL
CREATE TEMP FUNCTION jsonparse(input STRING)
RETURNS ARRAY<STRING>
LANGUAGE js AS """
  return JSON.parse(input).map(x=>JSON.stringify(x));
"""; 
WITH `project.lz.json_file` AS (
  SELECT '''{
  "Combos": [  {
    "Id": "1111",
    "Type": 0,
    "Description": "ABCD",
    "ComboDuration": {
      "StartDate": "2009-10-26T08:00:00",
      "EndDate": "2009-10-29T08:00:00"
    }  },  {
    "Id": "2222",
    "Type": 1,
    "Description": "XYZ",
    "ComboDuration": {
      "StartDate": "2019-10-26T08:00:00",
      "EndDate": "2019-10-29T08:00:00"
    }  },  {
    "Id": "39933",
    "Type": 3,
    "Description": "General",
    "ComboDuration": {
      "StartDate": "2019-10-26T08:00:00",
      "EndDate": "2019-10-29T08:00:00"
    }  },  {
    "Id": "39934",
    "Type": 2,
    "Description": "ABCDXYZ",
    "ComboDuration": {
      "StartDate": "2019-10-26T08:00:00",
      "EndDate": "2019-10-29T08:00:00"
    }  }]}  ''' AS conv_column
)
SELECT
  JSON_EXTRACT_SCALAR(combo, '$.Id') AS Id,
  JSON_EXTRACT_SCALAR(combo, '$.Type') AS Type,
  JSON_EXTRACT_SCALAR(combo, '$.Description') AS Description,
  JSON_EXTRACT_SCALAR(combo, '$.ComboDuration.StartDate') AS StartDate,
  JSON_EXTRACT_SCALAR(combo, '$.ComboDuration.EndDate') AS EndDate
FROM `project.lz.json_file`,
UNNEST(jsonparse(JSON_EXTRACT(conv_column, '$.Combos'))) combo

with output带输出

Row Id      Type    Description StartDate           EndDate  
1   1111    0       ABCD        2009-10-26T08:00:00 2009-10-29T08:00:00  
2   2222    1       XYZ         2019-10-26T08:00:00 2019-10-29T08:00:00  
3   39933   3       General     2019-10-26T08:00:00 2019-10-29T08:00:00  
4   39934   2       ABCDXYZ     2019-10-26T08:00:00 2019-10-29T08:00:00  

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

相关问题 如何在 BigQuery 中使用 python UDF 或 json_extract 查询 json? - How to query json with python UDF or json_extract in BigQuery? 我想用 BigQuery 提取 Json 格式的数据。 UDF 或 json_extract - I want to extract Json format data with BigQuery. UDF or json_extract 从 Big-query 中的 Complex JSON 中提取数据 - Extract data from Complex JSON in Big-query Bigquery - json_extract 数组中的所有元素 - Bigquery - json_extract all elements from an array 来自字符串字段的 AWS Athena json_extract 查询返回空值 - AWS Athena json_extract query from string field returns empty values 如何从 JSON 数据中提取特定的键值? - How to extract specific key values from JSON data? 如何使用(与物化视图兼容)SQL 从具有多个键值列表的 JSON 中提取值作为列? - How to extract a value as a column from JSON with multiple key-value lists using (a materialized view compatible) SQL? 使用 Node JS 从嵌套的 JSON 中提取数据 - Extract data from nested JSON using Node JS 使用 json_extract_array 提取嵌套的 JSON:在填充的数据字段上获取 null 结果? - Extracting nested JSON using json_extract_array: getting null results on populated data fields? 如何从 Flutter 的列表中提取 JSON? - How to extract JSON from a List in Flutter?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM