Serialized JSON with sorted keys, using Jackson Serialized JSON with sorted keys, using Jackson json json

Serialized JSON with sorted keys, using Jackson


As stated by @tim_yates, this doesn't work for map keys.

You could use

mapper.configure(SerializationConfig.Feature.ORDER_MAP_ENTRIES_BY_KEYS, true)

With newer version ( >= 2.6.1) the API changed to:

mapper.configure(SerializationFeature.ORDER_MAP_ENTRIES_BY_KEYS, true);


The documentation for SORT_PROPERTIES_ALPHABETICALLY explicitly says:

Feature that defines default property serialization order used for POJO fields (note: does not apply to Map serialization!)

So I guess you will need to change your input Map (as you say)


As pointed out, this feature just works for POJOs. However, I think there is a feature request to do the same for Maps, at Jackson Jira; and if not, this sounds like a good addition.

But in the meantime I would second @tim_yates suggestion to use intermediate TreeMap for sorting, serializing that: ordering that Map has will be used as is, so this should work.