Clever way to order a json item by key in postgresql (two examples) I need to sort a json item in a record, so here are two examples:
First case
create table jsontable (
    jsonitem json
);

Next I insert a json item:
insert into jsontable(jsonitem) values ('{ &quot;3&quot; : &quot;foo&quot;, &quot;2&quot; : &quot;bar&quot;, &quot;1&quot; : &quot;qux&quot; }');

Then I query the values:
select t.jsonitem from jsontable t;
                 jsonitem                  
-------------------------------------------
 { &quot;3&quot; : &quot;foo&quot;, &quot;2&quot; : &quot;bar&quot;, &quot;1&quot; : &quot;qux&quot; }
(1 row)

Let's suppose the key is unique and is an integer &gt; 0;
Question 1: ¿ How to sort the json item values by the key value, and get the following ?
{ &quot;1&quot; : &quot;qux&quot;, &quot;2&quot; : &quot;bar&quot;, &quot;3&quot; : &quot;foo&quot; }

Second case
This case use named keys and named values.
create table jsontable_arr(
   jsonitem json
   );

insert into jsontable_arr(jsonitem) values ('[  { &quot;key&quot; : &quot;3&quot; , &quot;value&quot;: &quot;foo&quot;}, { &quot;key&quot; : &quot;2&quot; , &quot;value&quot;: &quot;bar&quot;}, { &quot;key&quot; : &quot;1&quot; , &quot;value&quot;: &quot;qux&quot;} ]');

select t.* from jsontable_arr t;
                                                jsonitem
--------------------------------------------------------------------------------------------------------
 [  { &quot;key&quot; : &quot;3&quot; , &quot;value&quot;: &quot;foo&quot;}, { &quot;key&quot; : &quot;2&quot; , &quot;value&quot;: &quot;bar&quot;}, { &quot;key&quot; : &quot;1&quot; , &quot;value&quot;: &quot;qux&quot;} ]
(1 row)

Question 2: ¿ How to sort the json array item by the key value, and get the following?
[  { &quot;key&quot; : &quot;1&quot; , &quot;value&quot;: &quot;qux&quot;}, { &quot;key&quot; : &quot;2&quot; , &quot;value&quot;: &quot;bar&quot;}, {&quot;key&quot; : &quot;3&quot; , &quot;value&quot;: &quot;foo&quot;} ]

Thanks for your suggestions.
Be aware that order of array items is a different issue than order of keys in JSON object. See:

How to sort objects in an array inside a json or jsonb value by a property of the objects?

For your first example (implying integer sort), simply casting to jsonb would do the job. It happens to sort keys the way you want by default:
SELECT jsonitem::jsonb FROM jsontable;

db&lt;&gt;fiddle here
Or use jsonb instead of json to begin with.
However, it's not strictly integer sort. Keys are strings. Shorter keys sort before longer keys, which works as desired with only digits. But not necessarily with anything else. Like negative numbers:
2, 3, -1

Then again, your solution does string sort according to the default collation, i.e. breaks integer sort for numbers not comprised of the same number of digits:
111, 22, 3

You didn't specify exactly. This would do integer sort properly:
select id, json_object_agg(key, value) AS jsonitem
from  (
   select id, key, value
   from   tbl
   ORDER  BY id, key::int  -- !!!
   ) tbl
group  by id;

Of course, the cast to integer raises an exception for any string that's not a legal integer representation.
Sorting in a subquery once as this is typically much faster than the per-item sort you get from an added ORDER BY in an aggregate function. This works, because, quoting the manual:

Alternatively, supplying the input values from a sorted subquery will usually work.

See:

Is it REALLY possible that the order will not be guaranteed for this particular redundant derived table?
Customize jsonb key sort order involving arrays

Your second example is a different beast, trying to sort an array with JSON objects by the (single!?) nested key. You could reorder arrays like this:
SELECT j.id, arr.js_sorted
FROM   jsonarr j
CROSS  JOIN LATERAL (
   SELECT json_agg(value) AS js_sorted
   FROM (
      SELECT *
      FROM   json_array_elements(j.jsonitem)
      ORDER  BY (value-&gt;&gt;'key')::int  -- true integer sort!
      ) sub
   ) arr;

It really depends on where your values come from, what they can be exactly, and how you want to sort exactly.