【问题标题】:Iterate over ARRAY<JSON> in BigQuery在 BigQuery 中迭代 ARRAY<JSON>
【发布时间】:2021-06-21 07:58:54
【问题描述】:

我想从 BigQuery 中的 JSON 对象中获取值。我有一个像下面这样的 JSON

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

获取json对象值的条件是:如果apples = 2则橙子的返回值为4

如何在 BigQuery 中遍历 ARRAY?

【问题讨论】:

    标签: json google-cloud-platform google-bigquery user-defined-functions


    【解决方案1】:

    考虑下面的例子

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

    有输出

    【讨论】:

      猜你喜欢
      • 2021-05-12
      • 2023-01-13
      • 2021-08-06
      • 1970-01-01
      • 2021-11-25
      • 1970-01-01
      • 2017-11-02
      • 1970-01-01
      • 2023-02-09
      相关资源
      最近更新 更多