I am trying to build a jsonb object with keys ordered alphabetically.
However it seems jsonb_object_agg(k, v)
disregards the order of inputs and sorts the input by key length and then alphabetically.
E.g.
select jsonb_object_agg(k, v order by k) from (
values ('b', 'b_something'), ('ab', 'ab_something')
) as t (k,v)
gives
{
"b": "b_something",
"ab": "ab_something"
}
but what I need is
{
"ab": "ab_something"
"b": "b_something",
}
Is there a way to achieve this?
Context I am flattening a json column, where the contents follow a uniform but unwieldy schema. I have succeeded in doing so thanks to this useful answer, but the order of the keys is not how I need them.