【问题标题】:How to check if the value of key is ARRAY or STRUCT in BigQuery如何在 BigQuery 中检查键的值是 ARRAY 还是 STRUCT
【发布时间】:2021-06-23 14:22:28
【问题描述】:

我正在尝试从 JSON 对象中提取值,但我不知道该值是数组还是对象,因为我想编写一个通用代码来检查数组或对象并相应地返回值。

我有两个 JSON 如下

JSON 一:

"fruit":[{"apples":5,"oranges":10},{"apples":2,"oranges":4}]

JSON 二:

"fruit":{"apples":5,"oranges":10}

我必须得到oranges 的值,但fruit 的值可能会改变。 如何在 BigQuery 中检查值的类型并提取值?

【问题讨论】:

    标签: json google-cloud-platform google-bigquery


    【解决方案1】:

    考虑下面

    #standardSQL
    with `project.dataset.table` as (
      select 1 id, '{"fruit":[{"apples":5,"oranges":10},{"apples":2,"oranges":4}]}' json union all 
      select 2, '{"fruit":{"apples":5,"oranges":10}}'
    )
    select id,
      json_extract(x, '$.apples') apples,
      json_extract(x, '$.oranges') oranges
    from `project.dataset.table`, 
    unnest(ifnull(json_extract_array(json, '$.fruit'), [json_extract(json, '$.fruit')])) x           
    

    有输出

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-12-09
      • 1970-01-01
      • 1970-01-01
      • 2020-10-27
      • 2021-06-27
      • 1970-01-01
      • 2022-08-18
      • 1970-01-01
      相关资源
      最近更新 更多