SQLite json() Function

Savigo
You organize your data. Now organize your savings.
Set savings goals, track your progress, and stay on course.
Get Savigo for iPhone 

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)

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   }');

Output:

{"name":"Alice","age":25}

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}');

The JSON in this example misses a comma between two properties "name" and "age".

Output:

Runtime error: malformed JSON

The following example uses the json() function with the NULL argument:

SELECT json(null);

Output:

json(null)
----------
NULL

Summary #

  • Use the json() function to validate and return the minified version of the input JSON value with unnecessary whitespace removed.

Was this tutorial helpful?