【问题标题】:Extract fields from json string converted from array从数组转换的json字符串中提取字段
【发布时间】:2020-09-03 15:09:33
【问题描述】:

我正在使用 bigquery SQL,我有一个数组,我使用以下方法将其转换为 json 字符串:

TO_JSON_STRING(array)

这给了我一个看起来像这样的字段:

[{"Key":"helpId","Value":"abcd1234"},{"Key":"userid","Value":"abc123"},{"Key":"accountid","Value":"ab12"}]

我正在尝试使用以下方法提取密钥:

 JSON_EXTRACT(json_string, '$.Key')

但它一直返回 null - 有什么想法吗?

【问题讨论】:

    标签: sql arrays json google-bigquery


    【解决方案1】:

    以下是 BigQuery 标准 SQL

    #standardSQL
    WITH `project.dataset.table` AS (
      SELECT '[{"Key":"helpId","Value":"abcd1234"},{"Key":"userid","Value":"abc123"},{"Key":"accountid","Value":"ab12"}]' json_string
    )
    SELECT 
      JSON_EXTRACT_SCALAR(kv, '$.Key') key, 
      JSON_EXTRACT_SCALAR(kv, '$.Value') value 
    FROM `project.dataset.table`, 
    UNNEST(JSON_EXTRACT_ARRAY(json_string)) kv   
    

    有输出

    Row key         value    
    1   helpId      abcd1234     
    2   userid      abc123   
    3   accountid   ab12       
    

    希望您可以轻松地将上述示例应用于您的特定用例

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-01-01
      • 1970-01-01
      • 2016-02-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-10-13
      • 1970-01-01
      相关资源
      最近更新 更多