Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I get jq to pretty print json ordering keys alphabetically

Tags:

json

jq

I use jq to pretty print very complicated json. then use diff to compare different version. Is there a way I can get jq to order the output alphabetically by keys?

faster xml object mappers have support for this

prettyPrintObjectMapper = new ObjectMapper();
prettyPrintObjectMapper.configure(SerializationFeature.INDENT_OUTPUT, true); //turn on
prettyPrintObjectMapper.configure(SerializationFeature.ORDER_MAP_ENTRIES_BY_KEYS, true);

String tmp1 = prettyPrintObjectMapper.writeValueAsString(myObject);

Kind regards

Andy

like image 868
AEDWIP Avatar asked Jul 05 '16 18:07

AEDWIP


People also ask

How to sort JSON with jq?

Sorting JSON by value with jq can easily be done by using the sort_by() function. The main trick with using the sort_by() function is that your JSON input must be in an array. There are different ways to do this in jq (if your data is not already like this), including using the -s or --slurp option.


1 Answers

Use the -S flag to format the output like that:

--sort-keys / -S:

Output the fields of each object with the keys in sorted order.

like image 76
Hans Z. Avatar answered Oct 16 '22 14:10

Hans Z.