SQLite 3 reference · JSON Functions
SQLite JSON_GROUP_OBJECT() Function
Aggregate key/value pairs from the rows of a group into one JSON object, such as a name-to-price lookup.
SQLite 3 Since 3.10.0
Syntax
json_group_object(key, value)
Parameters
| Parameter | Description |
|---|---|
key |
The key for each row. |
value |
The value for each row. |
Return value
JSON object text.
Examples
SELECT json_group_object(name, price) AS prices
FROM products
WHERE category_id = 1;
Output
prices
{"SQL for Beginners":29.99,"Clean Code":37.5,"The Pragmatic Programmer":42.0}SELECT json_group_object(status, n) AS counts
FROM (SELECT status, count(*) AS n FROM orders GROUP BY status);
Output
counts
{"cancelled":1,"delivered":7,"pending":2,"shipped":2}Version history
| Version | Change |
|---|---|
| SQLite 3.10.0 | Added |
In other languages
JSONB_OBJECT_AGG()· PostgreSQL
Related
JSON_GROUP_ARRAY()· Aggregate the values of a group into one JSON array, one element per row. SQL NULLs become JSON null.JSON_OBJECT()· Build a JSON object from key and value pairs. SQL NULL becomes JSON null; nested json() values stay JSON.- SQL String Functions · tutorial