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?
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