Jackson JsonNode to string with sorted keys

This is the easiest way to do it, as provided by one of Jackson’s authors. There’s currently no way to go straight from JsonNode to String with sorted keys.

private static final ObjectMapper SORTED_MAPPER = new ObjectMapper();
static {
    SORTED_MAPPER.configure(SerializationFeature.ORDER_MAP_ENTRIES_BY_KEYS, true);
}

private String convertNode(final JsonNode node) throws JsonProcessingException {
    final Object obj = SORTED_MAPPER.treeToValue(node, Object.class);
    final String json = SORTED_MAPPER.writeValueAsString(obj);
    return json;
}

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)