Using postgres 9.4.
Data:
+-----+------+
| id | type |
+-----+------+
| 1 | A |
+-----+------+
| 2,3 | A |
+-----+------+
| 4 | B |
+-----+------+
Desired output (JSON):
[
[{"id": "1", "type": "A"}],
[{"id": "2", "type": "A"},{"id": "3", "type": "A"}],
[{"id": "4", "type": "B"}]
]
I've tried:
SELECT array_to_json(array_agg(c))
FROM
(
SELECT
regexp_split_to_table(id, ',') AS id,
type
FROM my_table
) c;
which gets me to a simple array of json objects:
[
{"id": "1", "type": "A"},
{"id": "2", "type": "A"},
{"id": "3", "type": "A"},
{"id": "4", "type": "B"}]
]
How do I wrap each resultset (not each row) of the subquery in an array?