'recursive tree as a list (array items) in a new attribute value child

How to get hierarchy data(recursive tree) as a new column like below? (if there is last child then column child array is empty)

rows: [{
  'id' : 1,
  'parent_id': null,
  'name': a
  'child': [{
    'id' : 2,
    'parent_id': 1,
    'name': a1,
    'child': ...
  }]
}]

WITH RECURSIVE t AS (
SELECT t.id AS id FROM category AS t WHERE parent_id is null
UNION ALL
SELECT child.id FROM category AS child JOIN t ON t.id = child.parent_id
) 
SELECT * FROM category WHERE id IN (SELECT * FROM t);

https://www.db-fiddle.com/f/ufnG1WpBX4Z8jsBEg6bsLs/4

UPDATE
because I do it with node-postgres it is return json already so I made another version use row_to_json for easier to understand my question

WITH RECURSIVE t AS (
SELECT t.id AS id FROM category AS t WHERE parent_id = 1
UNION ALL
SELECT child.id FROM category AS child JOIN t ON t.id = child.parent_id
) 
SELECT row_to_json(row) FROM (
  SELECT * FROM category WHERE id IN (SELECT * FROM t)
) row;

https://www.db-fiddle.com/f/ufnG1WpBX4Z8jsBEg6bsLs/5

it returns data like below

[
{"id":3,"parent_id":1,"name":"a1"},
{"id":4,"parent_id":3,"name":"a2"}
]

expected output

{"id":3,"parent_id":1,"name":"a1", "child": [{"id":4,"parent_id":3,"name":"a2"}]}


Solution 1:[1]

 SELECT row_to_json(row) FROM (
      SELECT * FROM category WHERE id IN (SELECT * FROM t::json as something)
    ) row;

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1