Skip to main content

JSON_VALID

Tests whether a string is well-formed JSON. JSON_ERROR_POSITION returns the position of the first syntax error instead.

See also:
[SELECT] [JSON] [JSON_TYPE]


Syntax​

SELECT JSON_VALID(value) FROM <multipartIdentifier>;
SELECT JSON_ERROR_POSITION(value) FROM <multipartIdentifier>;

Arguments​

value
The string to test.

Return Value(s)​

JSON_VALID returns 1 for well-formed JSON (RFC 8259) and 0 otherwise, including for JSON5 such as unquoted keys; the result is NULL for NULL. JSON_ERROR_POSITION returns 0 for valid JSON, otherwise the one-based character position of the first error.


Examples​

Test values​

The second value has an unquoted key, so it is not valid JSON:

SELECT JSON_VALID('{"a": 1}') AS a, JSON_VALID('{a: 1}') AS b, JSON_VALID(NULL) AS c;
|---|---|------|
| a | b | c |
|---|---|------|
| 1 | 0 | null |
|---|---|------|

Locate a syntax error​

The second value is missing a value after the colon:

SELECT JSON_ERROR_POSITION('{"a": 1}') AS a, JSON_ERROR_POSITION('{"a": }') AS b;
|---|---|
| a | b |
|---|---|
| 0 | 6 |
|---|---|

For more information, see https://www.sqlite.org/json1.html#jvalid