I have the following json format. It's basically a representation of the calendar:
{"month1": {"day1":{...}, "day2": {...}}, "month2": {...}}
I'd like to sort it such that both the months and the days will be in numeric order. The below input is the output of jq -S
, but as you see it's sorted like: 1,10,2, and I'd like to sort it like 1,2,10. How can I do that with jq?
Input:
{
"1":{"1":{"foo":"bar"},"10":{"foo":"bar"},"2":{"foo":"bar"}},
"10":{"1":{"foo":"bar"},"10":{"foo":"bar"},"2":{"foo":"bar"}},
"2":{"1":{"foo":"bar"},"10":{"foo":"bar"},"2":{"foo":"bar"}}
}
Expected output:
{
"1":{"1":{"foo":"bar"},"2":{"foo":"bar"},"10":{"foo":"bar"}},
"2":{"1":{"foo":"bar"},"2":{"foo":"bar"},"10":{"foo":"bar"}},
"10":{"1":{"foo":"bar"},"2":{"foo":"bar"},"10":{"foo":"bar"}}
}
update:
I want to have this sorting for 2 reasons:
- it's nice to have it ordered in the "human logical" way.
- I'd like to be able to diff 2 similar jsons in this format. (I guess this would work even with the current order)
For all the downvoters: jq -S
wasn't invented by me and it does sort the keys of an object. Many use jq as a json prettifier/formatter/identer, so I think this is a legitimate use-case. You don't have to sort your json objects, that's fine, but why down-voting?