The table I'm querying looks like this:
namespace | key | value
---------------------------
foo | bar | baz
foo | alpha | beta
gamma | delta | epsilon
And I'd like to pull it out of the database like this:
{
"foo": {
"bar": "baz",
"alpha": "beta"
},
"gamma": {
"delta": "epsilon"
}
}
Playing around with json_object_agg isn't really getting me past the first level, since you're not allowed to nest aggregate functions. But as far as I can see, I need a GROUP BY within a GROUP BY, but I'm not sure if that's possible. Perhaps the solution has to do with WINDOWs?