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
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With