How do I create JSON(B) from this statement:
SELECT *
FROM foo
LEFT JOIN bar
USING (id)
My solution currently looks like this:
SELECT to_jsonb(foo) || coalesce(to_jsonb(bar), '{}')
FROM foo
LEFT JOIN bar
USING (id)
This becomes uglier for every joined table, e.g.:
SELECT to_jsonb(foo) || coalesce(to_jsonb(bar), '{}') || coalesce(to_jsonb(baz), '{}')
FROM foo
LEFT JOIN bar
USING (id)
LEFT JOIN baz
USING (id)
I'd like something like this:
SELECT to_jsonb(*)
FROM foo
LEFT JOIN bar
USING (id)
But that gives me:
[42883] ERROR: function to_jsonb() does not exist