SQLite JSON_GROUP_OBJECT()

The SQLite json_group_object() function is an aggregate function that returns a JSON object comprised of all name/value pairs in the aggregation.

In other words, it constructs a JSON object from the values provided by its arguments.

Syntax

json_group_object(NAME,VALUE)

Where NAME, VALUE represents the name/value pairs to be used in the resulting JSON object.

Example

Suppose we have the following table:

SELECT PetId, PetName 
FROM Pets;

Result:

+-------+---------+
| PetId | PetName |
+-------+---------+
| 1     | Homer   |
| 2     | Yelp    |
| 3     | Fluff   |
| 4     | Brush   |
+-------+---------+

We can use json_group_object() to output that result as a JSON document that contains name/value pairs based on the columns in the table:

SELECT json_group_object(PetId, PetName)
FROM Pets;

Result:

+--------------------------------------------------+
|        json_group_object(PetId, PetName)         |
+--------------------------------------------------+
| {"1":"Homer","2":"Yelp","3":"Fluff","4":"Brush"} |
+--------------------------------------------------+