How to create an empty JSON object in postgresql?
Use left join
with coalesce()
. As default value use '{}'::json
.
select name, coalesce(d.data, '{}'::json) as datafrom meta mleft join ( select fk_id, json_object_agg(d.key, d.value) as data from data d group by 1 ) don m.id = d.fk_id; name | data ---------+------------------------------------ Florian | { "age" : "25" } Marcus | { "age" : "25", "color" : "blue" } Thomas | {}(3 rows)