Summary: in this tutorial, you will learn how to use the SQLite json()
function to validate a JSON value and return a minified JSON value with unnecessary whitespace removed.
Introduction to SQLite json() function
The json()
function validates a JSON value and returns its minified version with unnecessary whitespace removed.
json(json_value)
Code language: SQL (Structured Query Language) (sql)
In this syntax:
json_value
is the JSON value that you want to validate.
If the json_value
is valid, the json()
function returns the minified version of the JSON value. Otherwise, it throws an error.
The json()
function returns NULL
if the json_value
is NULL
.
SQLite json() function examples
The following example uses the json()
function to validate and return the minified version of a JSON value:
SELECT json('{"name": "Alice" ,"age" : 25 }');
Code language: SQL (Structured Query Language) (sql)
Output:
{"name":"Alice","age":25}
Code language: SQL (Structured Query Language) (sql)
In this example, the function returns the minified version of the input JSON value because the input JSON value is valid.
The following statement uses the json()
function to validate a JSON but returns an error because the input JSON value is invalid:
SELECT json('{"name": "Alice" "age" : 25}');
Code language: SQL (Structured Query Language) (sql)
The JSON in this example misses a comma between two properties "name"
and "age"
.
Output:
Runtime error: malformed JSON
Code language: SQL (Structured Query Language) (sql)
The following example uses the json()
function with the NULL
argument:
SELECT json(null);
Code language: SQL (Structured Query Language) (sql)
Output:
json(null)
----------
NULL
Code language: SQL (Structured Query Language) (sql)
Summary
- Use the
json()
function to validate and return the minified version of the input JSON value with unnecessary whitespace removed.