If I have a field jsonCol
which has list of json objects, for example:
[{'name': 'fieldA', 'enum': 'someValA'},
{'name': 'fieldB', 'enum': 'someValB'},
{'name': 'fieldC', 'enum': 'someValC'}]
Another row may look like:
[{'name': 'fieldA', 'enum': 'someValA'},
{'name': 'fieldC', 'enum': 'someValC'}]
How do I get rows where the fieldB
exists?
I have a query that can look for the value of fieldB
, howver the query fails in cases when fieldB
doesn’t exist with the error:
Error running query: Array subscript must be less than or equal to array length: 1 > 0
My query:
SELECT
json_extract_scalar(filter(cast(json_parse(jsonCol) AS array(json)), x -> json_extract_scalar(x, '$.name') = 'fieldB')[1], '$.enum') AS myField
FROM myTable
WHERE
json_extract_scalar(filter(cast(json_parse(jsonCol) AS array(json)), x -> json_extract_scalar(x, '$.name') = 'fieldB')[1], '$.enum') = 'someValB'
How can I check for the value of someValB
but also ignore cases when the json doesn’t exist at all?
2
Answers
This is a working solution for
SQLite
.json_each()
used to returns row for each array element or object member.json_extract()
extracts and returns one or more values from json object.Result :
Demo here
Use
element_at
instead of array access with[1]
:Output:
Note that possibly you can simplify the query by moving some parts into
WITH
clause or subquery.If you can switch to Trino – you can use it’s improved json path support via
json_query
(orjson_exists
). TBH I don’t fully understand your goal but here are some options:Output: