【问题标题】:Extract components of a nested Array/STRUCT JSON string field in BigQuery在 BigQuery 中提取嵌套 Array/STRUCT JSON 字符串字段的组件
【发布时间】:2022-01-03 22:04:51
【问题描述】:

我有一个 JSON 格式的字符串字段,我试图从中提取条带拒绝代码。该字段的示例如下:

{"errors":[{"message":"Your card has insufficient funds.","type":"payment","code":"card_declined","decline_code":"insufficient_funds","gateway":"stripe","stripe":{"type":"card_error","code":"card_declined","decline_code":"insufficient_funds","message":"Your card has insufficient funds.","charge":"ch_3JodUAHkqql8g8ta1ADf5fBf"}}]}

我尝试了UNNEST 的各种组合,但仍然收到错误消息。我认为这个问题与该领域是各种 STRUCTS/Arrays 的组合但没有运气提取我需要的东西有关。任何帮助将不胜感激!

【问题讨论】:

    标签: arrays struct google-bigquery extract unnest


    【解决方案1】:

    我想我找到了解决办法。有点 hacky,但我在 CTE 中做了以下操作;

    REPLACE(REPLACE(JSON_EXTRACT(error_message, '$.errors'),"[",""),"]","") as struct_1
    

    然后又取了一个 JSON_EXTRACT;

    JSON_EXTRACT(struct_1,'$.stripe.decline_code')
    

    【讨论】:

    • 有创意但......在我的回答中看到非hacky方法
    【解决方案2】:

    考虑以下方法 - 非 hacky 一种:o)

    select json_extract_scalar(error, '$.stripe.decline_code') as decline_code
    from your_table, 
    unnest(json_extract_array(error_message, '$.errors')) error    
    

    如果应用于您问题中的样本数据 - 输出是

    【讨论】:

    • 你试过了吗?它对你有用吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-01-20
    • 2018-06-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-01-13
    • 1970-01-01
    相关资源
    最近更新 更多