简体   繁体   中英

JSON comparison function in Bigquery

Is there any way to have a valid JSON object comparison function in BigQuery. Here are a few examples of input/output I'd like to see:

# all should result in TRUE
SELECT JSON '1' < JSON '2'
SELECT JSON '9' < JSON '10'
SELECT JSON '"a"' < '"b"'
SELECT JSON '"D"' < JSON '"a"'
SELECT JSON 'false' < JSON 'true'

Perhaps it would need a JS udf or something as it involves type-interpolation...perhaps recursively, but seeing what might be possible here.

The example provided does not show any nested structure. Therefore, the solution will not include any recursion. The JSON have to be interpreted in different formats: numbers and strings. This cannot be done in SQL and a JavaScript UDF is needed.

create temp function json_is_smaller(a json, b json)
returns bool
language js as
"""
return a<b
""";
with tbl as (SELECT JSON '1' a , JSON '2' b
UNION ALL SELECT JSON '9' , JSON '10'
UNION ALL SELECT JSON '"a"' , JSON '"b"'
UNION ALL SELECT JSON '"D"' , JSON '"a"'
UNION ALL SELECT JSON 'false' , JSON 'true')

select *,  json_is_smaller(a,b)
from tbl

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM