Take the following schema:
CREATE TABLE my_table (
id INTEGER PRIMARY KEY,
json_data TEXT
);
The json_data column is of the following format:
[
{"key": "foo", "date": "2023-01-01"},
{"key": "bar", "date": "2023-01-02"}
]
I can write a query like this to get the date of the first element that has “key”: “bar”:
SELECT (
SELECT json_extract(value, '$.date')
FROM my_table,
json_each(my_table.json_data) AS each
WHERE json_extract(each.value, '$.key') = 'bar'
ORDER BY each.rowid
LIMIT 1
) AS first_foo_date
FROM my_table;
Is it possible to have this kind of use of JSON functions in a generated column?