SQLite JSON Functions: Extract, Update, Validate and Query JSON
SQLite JSON functions let you read, modify, validate, and aggregate JSON without adding a separate JSON column type. JSON text is usually stored as TEXT; SQLite 3.45.0 also introduced its own binary JSONB format stored as a BLOB. SQLite JSONB is not compatible with PostgreSQL JSONB.
Choose a JSON operation
| Task | Function | Notes |
|---|---|---|
| Read a value at a path | json_extract() |
A single path to a JSON string or number returns an SQL scalar, not necessarily JSON text |
| Update or add a value | json_set() |
Returns a new JSON value; it does not update a table row by itself |
| Check whether input is JSON | json_valid() |
Useful before querying untrusted JSON text |
| Expand an array or object into rows | json_each() |
Table-valued function; use in the FROM clause |
| Build a JSON array from rows | json_group_array() |
Aggregate function |
Example: extract and update JSON
Run these queries in the SQLite Playground:
SELECT
json_extract('{"name":"Ada","scores":[8,9]}', '$.name') AS name,
json_extract('{"name":"Ada","scores":[8,9]}', '$.scores[0]') AS first_score,
json_valid('{"ok":true}') AS is_valid;
The result is Ada, 8, and 1 (valid JSON).
SELECT json_set('{"name":"Ada"}', '$.active', json('true')) AS updated;
This returns {"name":"Ada","active":true}. The json('true') call inserts a JSON boolean rather than the string "true".
To turn a JSON array into SQL rows:
SELECT value
FROM json_each('[10,20,30]')
ORDER BY CAST(key AS INTEGER);
This returns three rows: 10, 20, and 30.
JSON text versus JSONB
JSONB can avoid repeatedly parsing JSON text, but it is SQLite-specific and its availability depends on the SQLite version. For storage and compatibility trade-offs, see SQLite JSONB: When and How to Use It. For cross-database JSON aggregation, see SQL JSON aggregation by database.
For hands-on testing, run these JSON queries in the SQLite Playground. To turn a JSON array of records into SQL INSERT statements, use the JSON to SQL converter.
Reference: SQLite JSON functions.
-
json
The SQLitejson()function validates the string specified by the parameter and converts it to a minimal JSON string, with all unnecessary whitespace removed. -
json_array
The SQLitejson_array()function evaluates all the values in the parameters list and returns a JSON array containing all the parameters. -
json_array_length
The SQLitejson_array_length()function returns the length of elements in the specified JSON array, that is the number of the top level child elements in the JSON array. -
json_each
Use SQLite json_each() to turn JSON arrays into rows, filter table records by JSON array values, and avoid malformed JSON errors with json_valid(). -
json_extract
Learn SQLite json_extract() paths, return types, JSON null versus missing values, multiple paths, and differences from MySQL with runnable SQL examples. -
json_group_array
The SQLitejson_group_array()function is an aggregate function that returns a JSON array containing all the values in a group. -
json_group_object
The SQLitejson_group_object()function is an aggregate function that returns a JSON object containing the key-value pairs of the specified columns in a group. -
json_insert
The SQLitejson_insert()function inserts values into a JSON document and return a new JSON document. -
json_object
The SQLitejson_object()function returns a JSON object containing all the key-value pairs specified by the parameters. -
json_patch
The SQLite json_patch() function merges and patchs the second JSON object to the original JSON object, and returns the patched original JSON object. -
json_quote
The SQLitejson_quote()function converts the SQL value specified by the parameter to the corresponding JSON representation. -
json_remove
The SQLitejson_remove()function removes the data specified by a path from a JSON document and returns the modified JSON document. -
json_replace
The SQLitejson_replace()function replaces existing data in a JSON document and return modified JSON document. -
json_set
Learn SQLite json_set() with runnable examples for JSON object updates, array appends, and nested JSON values. -
json_tree
Use SQLite json_tree() to recursively query JSON values and inspect each row’s key, value, type, atom, fullkey, and parent. -
json_type
Find the SQLite JSON type at any JSON path; distinguish JSON null from a missing key and compare json_type() with typeof() using SQL examples. -
json_valid
Check JSON text, JSON5 and SQLite JSONB with json_valid(). Learn flags 1, 2, 6 and 8, NULL behavior, versions, and runnable SQL examples. -
jsonb
Convert JSON text to SQLite’s binary JSONB BLOB with jsonb(), or pass through an input that appears to be JSONB. -
jsonb_each
Use SQLite jsonb_each() to expand one JSON object or array level, returning nested objects and arrays as JSONB values. -
jsonb_tree
Use SQLite jsonb_tree() to recursively expand JSON, returning nested objects and arrays as JSONB values.