JSON Functions
Turso provides a full set of JSON functions compatible with SQLite’s JSON1 extension. These functions operate on JSON stored as TEXT or in Turso’s internal binary JSON (JSONB) format. Most functions come in pairs: ajson_* variant that returns TEXT and a jsonb_* variant that returns BLOB in the internal binary format. The JSONB variants are more efficient when the result will be stored or passed to another JSON function rather than returned to the application.
JSON Path Syntax
Many JSON functions accept a path argument that identifies a specific element within a JSON document.
Path arguments must begin with
$. If a path does not match any element, functions generally return NULL.
JSON Creation and Validation
json
Validates a JSON string and returns it in minified form. If the input is not valid JSON, an error is raised.
Returns: TEXT — the minified JSON string.
jsonb
Converts a JSON string to the internal binary JSON format.
Returns: BLOB — the value in binary JSON format.
json_array / jsonb_array
Creates a JSON array from the arguments.
Returns: TEXT (json_array) or BLOB (jsonb_array) — a JSON array.
json_object / jsonb_object
Creates a JSON object from alternating label/value pairs. When called with*, expands all columns of the row into label/value pairs using column names as keys.
Returns: TEXT (json_object) or BLOB (jsonb_object) — a JSON object.
json_quote
Converts a SQL value to its JSON representation.
Returns: TEXT — the JSON representation of the value.
json_valid
Returns 1 if the argument is well-formed JSON, or 0 otherwise.
Returns: INTEGER — 1 if valid JSON, 0 otherwise.
json_error_position
Returns the character position of the first syntax error in a JSON string, or 0 if the string is valid JSON.
Returns: INTEGER — character position of the first error (1-based), or 0 if valid.
JSON Extraction
json_extract / jsonb_extract
Extracts one or more values from a JSON document using path arguments.
Returns: With a single path, returns the extracted value using its natural SQL type (INTEGER, REAL, TEXT, or NULL). JSON objects and arrays are returned as TEXT. With multiple paths, returns a JSON array of the extracted values.
jsonb_extract returns BLOB.
-> operator
Extracts a value from JSON and returns it as JSON. This is a shorthand forjson_extract that always returns JSON text (objects and arrays remain as JSON, strings are JSON-quoted).
Returns: TEXT — the extracted value as JSON.
->> operator
Extracts a value from JSON and returns it as a SQL value. Strings are unquoted, numbers are returned as INTEGER or REAL, and booleans are returned as integers (0 or 1).
Returns: The extracted value as its natural SQL type (TEXT, INTEGER, REAL, or NULL).
json_type
Returns the type of a JSON value as a string:"null", "true", "false", "integer", "real", "text", "array", or "object".
Returns: TEXT — the JSON type name.
JSON Modification
json_insert / jsonb_insert
Inserts new values into a JSON document. Existing values are not overwritten. If the path already exists, the value is left unchanged.
Returns: TEXT (json_insert) or BLOB (jsonb_insert) — the modified JSON.
json_replace / jsonb_replace
Replaces existing values in a JSON document. If the path does not exist, no insertion is made.
Returns: TEXT (json_replace) or BLOB (jsonb_replace) — the modified JSON.
json_set / jsonb_set
Inserts or replaces values in a JSON document. Combines the behavior ofjson_insert and json_replace: if the path exists, the value is replaced; if it does not exist, the value is inserted.
Returns: TEXT (json_set) or BLOB (jsonb_set) — the modified JSON.
json_remove / jsonb_remove
Removes one or more elements from a JSON document.
Returns: TEXT (json_remove) or BLOB (jsonb_remove) — the modified JSON.
json_patch / jsonb_patch
Applies an RFC 7396 merge patch to a JSON document. Object members in the patch overwrite members in the target. Anull value in the patch removes the corresponding member.
Returns: TEXT (json_patch) or BLOB (jsonb_patch) — the patched JSON.
json_pretty
Returns a pretty-printed (indented) representation of a JSON document.
Returns: TEXT — the formatted JSON string with indentation.
JSON Array Functions
json_array_length
Returns the number of elements in a JSON array. Returns 0 for an empty array and NULL for non-array JSON values.
Returns: INTEGER — the number of elements, or NULL if the value at the path is not an array.
JSON Aggregate Functions
json_group_array / jsonb_group_array
Aggregate function that collects values from a group into a JSON array.
Returns: TEXT (json_group_array) or BLOB (jsonb_group_array) — a JSON array of all values in the group.
json_group_object / jsonb_group_object
Aggregate function that collects label/value pairs from a group into a JSON object.
Returns: TEXT (json_group_object) or BLOB (jsonb_group_object) — a JSON object.
JSON Table-Valued Functions
json_each
A table-valued function that walks the top-level elements of a JSON array or object, returning one row per element.
Output columns:
json_tree
A table-valued function that recursively walks a JSON document, returning one row for every element at every level of nesting.
Output columns: Same as
json_each.
Practical Examples
Storing and querying JSON data
Modifying JSON in place
Building JSON from relational data
Flattening JSON arrays with json_each
See Also
- Data Types for how JSON values map to SQL types
- Expressions for using JSON operators in expressions