Order specific columns in json_agg
Without knowing exactly what you meant by 'The "distinct" does work the "order by" does not.', I guess you get an error:


  ERROR:  in an aggregate with DISTINCT, ORDER BY expressions must appear in argument list


It is more or less self-explanatory.  Try 

json_agg(DISTINCT (colA, colB) ORDER BY (colA, colB))


instead - you don't aggregate individual columns in json_agg() but a record, and should order by that one.