SELECT menu_main.id,
menu_main.name,
( SELECT array_to_json(array_agg(row_to_json(child.*))) AS array_to_json
FROM ( SELECT menu_second.type,
menu_second.name,
(menu_main.link::text || '-'::text) || menu_second.link::text AS link
FROM menu_second
WHERE menu_second.mainmenu = menu_main.id
ORDER BY menu_second.orders) child) AS items
FROM menu_main;
declare
ret json;
begin
ret := json_dyn.executeObject('select * from tab');
ret.print;
end;
/