【问题标题】:Extract value from JSON with key name includes # in bigquery从带有键名的 JSON 中提取值在 bigquery 中包含 #
【发布时间】:2018-04-03 19:57:04
【问题描述】:

我在 bigquery 中有一个名为 test 的表。它有一列称为attributes。 该值采用 JSON 格式,以“# of items”为键,例如:{“# of items”:“100”}

当我执行下面的查询时 SELECT JSON_EXTRACT(attributes, "$['# of items']") AS num_items FROM test

我遇到了以下错误: Error: JSONPath parse error at: ['# of items']

获取其价值的最简单方法是什么?

【问题讨论】:

    标签: json google-bigquery


    【解决方案1】:

    BigQuery Legacy SQL 失败

    改用 BigQuery 标准 SQL 就可以了

    #standardSQL
    WITH `project.dataset.test` AS (
      SELECT '{"# of items": "100"}' AS attributes
    )
    SELECT JSON_EXTRACT(attributes, "$['# of items']") AS num_items
    FROM `project.dataset.test`   
    

    结果为

    Row num_items    
    1   "100"    
    

    如果由于某种原因你被绑定到 BQ Legacy SQL - 使用下面的技巧

    #legacySQL
    SELECT JSON_EXTRACT(REPLACE(attributes, '"# of items"', "'number of items'"), "$['number of items']") AS num_items
    FROM [project:dataset.test]
    

    【讨论】:

    • 当然。请考虑投票给答案
    • 但是SELECT '{"# of items": "100"}' 部分是硬编码的。如果我想要键 # of items 的所有值怎么办?这不是我的情况,但我可以了解它
    • 您是否介意将您的问题作为一个单独的问题发布 - 所以我们将能够回答它。 cmets 格式不允许这样做
    • 顺便说一句。您在 WITH 语句中看到的任何内容都只是一个虚拟数据 - 您可以删除该部分并针对您的真实表实际运行该语句
    • 我认为 WITH 块是强制性的。我终于明白了。谢谢
    猜你喜欢
    • 1970-01-01
    • 2023-01-22
    • 2020-09-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-06
    • 2022-09-23
    • 2023-03-08
    相关资源
    最近更新 更多