The general concept should work. You may at least wish to experiment in a simple query to get the feel for the mechanics. With respect to this query I'd probably need to have a working query to play with (and time) to give a more precise answer.
David J.
Thanks David,
I do not get what you suggest working within the left-join. What does work is the following:
select json_agg(_) from ( select vc.id, vc.descr, vc.expense, vc.version, (select coalesce(json_agg(_), '[]') from ( select vp.percentage, vp.version, json_build_object( 'lower', lower(vp.validity), 'upper', upper(vp.validity), 'lower_inc', lower_inc(vp.validity), 'upper_inc', upper_inc(vp.validity) ) validity from vat_percentage vp where vp.vatcat_id = vc.id ) _ ) as percentages from vat_cat vc group by vc.id ) _