BigQuery支持哪些JsonPath表达式?

时间:2017-10-14 07:26:50

标签: google-bigquery

我在BigQuery documentation中读到它支持JsonPath表达式语言的子集。但我找不到支持 支持的JsonPath的哪些部分?例如,当我在控制台中尝试时,我似乎无法在BigQuery的JsonPath表达式中使用通配符或过滤器。

  1. 是否可以在BigQuery中的JsonPath表达式中使用通配符和过滤器?
  2. 是否有参考文档或其他文档描述BigQuery中的完整 JsonPath支持(因为我似乎无法找到它)?

2 个答案:

答案 0 :(得分:6)

  

是否可以在BigQuery中的JsonPath表达式中使用通配符和过滤器?

为了克服JsonPath的BigQuery“限制”,可以引入custom function,如下例所示:
注意:它使用jsonpath-0.8.0.js,可以从https://code.google.com/archive/p/jsonpath/downloads下载并上传到Google云端存储 - gs://your_bucket/jsonpath-0.8.0.js

   
#standardSQL
CREATE TEMPORARY FUNCTION CUSTOM_JSON_EXTRACT(json STRING, json_path STRING)
RETURNS STRING
LANGUAGE js AS """
    try { var parsed = JSON.parse(json);
        return JSON.stringify(jsonPath(parsed, json_path));
    } catch (e) { return null }
"""
OPTIONS (
    library="gs://your_bucket/jsonpath-0.8.0.js"
);
WITH t AS (
SELECT '''
{ "store": {
        "book": [ 
            { "category": "reference",
                "author": "Nigel Rees",
                "title": "Sayings of the Century",
                "price": 8.95
            },
            { "category": "fiction",
                "author": "Evelyn Waugh",
                "title": "Sword of Honour",
                "price": 12.99
            },
            { "category": "fiction",
                "author": "Herman Melville",
                "title": "Moby Dick",
                "isbn": "0-553-21311-3",
                "price": 8.99
            },
            { "category": "fiction",
                "author": "J. R. R. Tolkien",
                "title": "The Lord of the Rings",
                "isbn": "0-395-19395-8",
                "price": 22.99
            }
        ],
        "bicycle": {
            "color": "red",
            "price": 19.95
        }
    }
}
''' AS x
)
SELECT 
    CUSTOM_JSON_EXTRACT(x, '$.store.book[*].author'),
    CUSTOM_JSON_EXTRACT(x, '$..*[?(@.price==22.99)].author'),
    CUSTOM_JSON_EXTRACT(x, '$..author'),
    CUSTOM_JSON_EXTRACT(x, '$.store.*'),
    CUSTOM_JSON_EXTRACT(x, '$.store..price'),
    CUSTOM_JSON_EXTRACT(x, '$..book[(@.length-1)]'),
    CUSTOM_JSON_EXTRACT(x, '$..book[-1:]'),
    CUSTOM_JSON_EXTRACT(x, '$..book[0,1]'),
    CUSTOM_JSON_EXTRACT(x, '$..book[:2]'),
    CUSTOM_JSON_EXTRACT(x, '$..book[?(@.isbn)]')
FROM t

结果如下

CUSTOM_JSON_EXTRACT(x, '$.store.book[*].author')

[
  "Nigel Rees"
  "Evelyn Waugh"
  "Herman Melville"
  "J. R. R. Tolkien"
]

CUSTOM_JSON_EXTRACT(x, '$..*[?(@.price==22.99)].author')

[
  "J. R. R. Tolkien"
]  

CUSTOM_JSON_EXTRACT(x, '$.store..price')

[
  8.95
  12.99
  8.99
  22.99
  19.95
]

依旧......

如您所见 - 现在您可以使用通配符和过滤器以及所有爵士乐:o)

答案 1 :(得分:3)

支持的元素位于您链接到的部分的表格中。具体来说,它包括$.[],其中后者可以是子运算符或下标(数组)运算符。如果未列出某些内容,则不支持。