AEDWIP
AEDWIP

Reputation: 948

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

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

Upvotes: 32

Views: 14587

Answers (1)

Hans Z.
Hans Z.

Reputation: 54038

As per the manual, 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.

Upvotes: 54

Related Questions