Prepare the report input
Original table stores valid object fragments as TEXT. Passing ordinary text to json_group_array quotes each fragment as a JSON string. json(fragment) supplies a JSON value, producing an array of objects in id order.
Every record and report rule below is original and fictional. Reproduce the setup in a fresh SQLite test database; input previews include every table in this small fixture.
| id | fragment |
|---|---|
| 1 | {"name":"A"} |
| 2 | {"name":"B"} |
CREATE TABLE fragments(id INTEGER PRIMARY KEY,fragment TEXT NOT NULL CHECK(json_valid(fragment) AND json_type(fragment)='object')); INSERT INTO fragments VALUES(1,'{"name":"A"}'),(2,'{"name":"B"}');Compare misleading and corrected results
SELECT json_group_array(fragment ORDER BY id) AS objects FROM fragments;| objects |
|---|
| ["{\"name\":\"A\"}","{\"name\":\"B\"}"] |
SELECT json_group_array(json(fragment) ORDER BY id) AS objects FROM fragments;| objects |
|---|
| [{"name":"A"},{"name":"B"}] |
The bad array needs another parse per element; the corrected array already contains objects A and B. Schema checks require each input fragment to be a valid object.
Check a fresh boundary case
Add {} as a third object. The export preserves the empty object as an element.
Recreate the original setup in another fresh database, then apply the change and corrected query below. For an empty result, the table displays a labelled empty-result marker rather than a data record.
INSERT INTO fragments VALUES(3,'{}');
SELECT json_group_array(json(fragment) ORDER BY id) AS objects FROM fragments;| objects |
|---|
| [{"name":"A"},{"name":"B"},{}] |
Is an empty object lost in the corrected export?
Show the worked answer
No. The boundary array contains A, B and {}, all as objects rather than quoted object strings.