SQLite JSON
JSON in SQLite
JSON is a popular text format for data. SQLite has no special JSON data type, but it stores JSON as text, and has many functions to work with it.
Note: The JSON functions are built into SQLite 3.38.0 and later. In older versions, they were an extension that was often, but not always, included.
Read a Value
json_extract() reads a value from JSON. The path $.name means the "name" value in the top object:
The -> and ->> Operators
->> is a shorter way to read a value. It returns an SQL value, like text or a number. -> returns the value as JSON:
The first column is the text Emil. The second column is the JSON text "Emil", with the quotes.
JSON in a Table
This example creates a table with a column that holds JSON, and selects the users that have logged in:
Example
CREATE TABLE IF NOT EXISTS Events (
EventID INTEGER PRIMARY KEY,
Data TEXT
);
INSERT INTO Events (Data) VALUES
('{"type": "login", "user": "Emil"}'),
('{"type": "logout", "user": "Nora"}'),
('{"type": "login", "user": "Linus"}');
SELECT Data ->> '$.user' AS User
FROM Events
WHERE Data ->> '$.type' = 'login';
Try it Yourself »
Create JSON
json_object() creates a JSON object from names and values:
Example
SELECT json_object('id', CustomerID,
'name', CustomerName)
FROM Customers;
Try it Yourself »
json_group_array() turns the values from many rows into one JSON array:
Change JSON
json_set() adds or changes a value, and returns the new JSON:
Read a JSON Array as Rows
json_each() is a table valued function. It returns one row for each value in a JSON array:
Tip: See all the JSON functions in our SQLite Functions Reference.