jsonpath in bigquery doesn't support @ for filter. Suggestions for alternatives?

孤者浪人 提交于 2020-01-06 06:25:47

问题


At https://jsonpath.curiousconcept.com/ with this JSON document:

[{
    "key": "name",
    "value": "john doe"
}, {
    "key": "age",
    "value": "8"
}, {
    "key": "gender",
    "value": "male"
}]

I can use JsonPath expression $[?(@.key=="age")].value to extract the value ["8"] which is what I want.

However, when I try and use that same JsonPath in bigquery like so:

select JSON_EXTRACT_SCALAR('[{"key": "name","value": "john-doe"}, {"key": "age","value": "8"}, {"key": "gender","value": "male"}]', '$[?(@.key=="age")].value')

I get an error

Unsupported operator in JSONPath: @

Any suggestions as to how to achieve this in bigquery?


回答1:


Below is for BigQuery Standard SQL

#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 `project.dataset.your_table` AS (
  SELECT '''
    [{
        "key": "name",
        "value": "john doe"
    }, {
        "key": "age",
        "value": "8"
    }, {
        "key": "gender",
        "value": "male"
    }]  
      ''' str
)
SELECT CUSTOM_JSON_EXTRACT(str, '$[?(@.key=="age")].value')
FROM `project.dataset.your_table`  

Note: you need to upload jsonpath-0.8.0.js library to your_bucket on Cloud Storage. It can be downloaded from https://code.google.com/archive/p/jsonpath/downloads.

Above approach overcomes BigQuery "limitation" for JsonPath and now you can use all 'regular' JsonPath features




回答2:


Had to resort to regex unfortunately :(

select regexp_extract('[{"key": "name","value": "john-doe"}, {"key": "age","value": "8"}, {"key": "gender","value": "male"}]', r'"key": "age","value": "([^,:]+)"')

I say "unfortunately" because I'm of the opinion that JsonPath would be a more elegant way of achieving this.



来源:https://stackoverflow.com/questions/51673083/jsonpath-in-bigquery-doesnt-support-for-filter-suggestions-for-alternatives

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!