SQLite 3 reference · JSON Functions
SQLite JSON_OBJECT() Function
Build a JSON object from key and value pairs. SQL NULL becomes JSON null; nested json() values stay JSON.
SQLite 3 Since 3.9.0
Syntax
json_object(key1, value1, ...)
Parameters
| Parameter | Description |
|---|---|
key, value |
Pairs of a text key and a value. |
Return value
JSON object text.
Examples
SELECT json_object('id', customer_id, 'name', name, 'email', email) AS customer
FROM customers
WHERE customer_id IN (4, 5);
Output
customer
{"id":4,"name":"Linus Torvalds","email":"[email protected]"}
{"id":5,"name":"Margaret Hamilton","email":null}SELECT json_object('tags', json_array('a', 'b'), 'raw', '[1,2]') AS mixed;
Output
mixed
{"tags":["a","b"],"raw":"[1,2]"}Version history
| Version | Change |
|---|---|
| SQLite 3.9.0 | Added |
| SQLite 3.38.0 | Built in by default; earlier versions needed the JSON1 extension. |
In other languages
JSONB_BUILD_OBJECT()· PostgreSQL
Related
JSON_ARRAY()· Build a JSON array from the arguments. SQL NULL becomes JSON null; numbers stay numbers.JSON_GROUP_OBJECT()· Aggregate key/value pairs from the rows of a group into one JSON object, such as a name-to-price lookup.JSON_GROUP_ARRAY()· Aggregate the values of a group into one JSON array, one element per row. SQL NULLs become JSON null.- SQL String Functions · tutorial