create table test_json_agg (f1 text, f2 hstore);
insert into test_json_agg values ('rec1','"a key" =>1, b => t, c => null, d=> 12345, e => 012345, f=> 1.234, g=> 2.345e+4'),
('rec2','"a key" =>2, b => f, c => "null", d=> -12345, e => 012345.6, f=> -1.234, g=> 0.345e-4');
-select json_agg(q) from test_json_agg q;
+select json_agg(q order by (f1)) from test_json_agg q;
json_agg
----------------------------------------------------------------------------------------------------------------------------
[{"f1":"rec1","f2":{"b": "t", "c": null, "d": "12345", "e": "012345", "f": "1.234", "g": "2.345e+4", "a key": "1"}}, +
{"f1":"rec2","f2":{"b": "f", "c": "null", "d": "-12345", "e": "012345.6", "f": "-1.234", "g": "0.345e-4", "a key": "2"}}]
(1 row)
-select json_agg(q) from (select f1, hstore_to_json_loose(f2) as f2 from test_json_agg) q;
+select json_agg(q order by f1) from (select f1, hstore_to_json_loose(f2) as f2 from test_json_agg) q;
json_agg
----------------------------------------------------------------------------------------------------------------------
[{"f1":"rec1","f2":{"b": true, "c": null, "d": 12345, "e": "012345", "f": 1.234, "g": 2.345e+4, "a key": 1}}, +
create table test_json_agg (f1 text, f2 hstore);
insert into test_json_agg values ('rec1','"a key" =>1, b => t, c => null, d=> 12345, e => 012345, f=> 1.234, g=> 2.345e+4'),
('rec2','"a key" =>2, b => f, c => "null", d=> -12345, e => 012345.6, f=> -1.234, g=> 0.345e-4');
-select json_agg(q) from test_json_agg q;
-select json_agg(q) from (select f1, hstore_to_json_loose(f2) as f2 from test_json_agg) q;
+select json_agg(q order by (f1)) from test_json_agg q;
+select json_agg(q order by f1) from (select f1, hstore_to_json_loose(f2) as f2 from test_json_agg) q;