Sort hash keys on JSON encode

Make sure JSON result stays the same for a given hash. This is necessary
for caching.