16

I am writing some RESTful services using spring MVC. I am using jsckson mapper to do the It conversions. It all works fine except that the json it produces has fields completely unordered.

for e.g. If my entity object looks like this:

public class EntityObj
{
   private String x;
   private String y;
   private String z;
}

If I now have a list of EntityObjs, and I return this back from the controller, the json has the order mixed up for the fields e.g.: [{y:"ABC", z:"XYZ", x:"DEF"},{y:"ABC", z:"XYZ", x:"DEF"}]

Looked around for a solution but not finding any. Anyone else faced this issue?

Thanks for the help

gotz
  • 539
  • 2
  • 8
  • 23
  • 3
    But why is this an issue? Whenever I used JSON I also noticed the weird ordering, but anyway when you access it later you will probably access by "key", so the order shouldn't matter. – YuviDroid Mar 07 '12 at 23:51
  • Good point that it should not be an issue, but I am curious why you would like to order them. DO you have a special reason to do so? – Koen Peters Mar 08 '12 at 00:24
  • well, just to make it readable. there is no issue parsing it. i have objects with list attributes. so say an objectA with a list of objectB inside it along with other attributes like id, name etc. When some one sees the json response from my API, i would rather have them see the id and the name and the list of ObjectBs after that. – gotz Mar 08 '12 at 01:26
  • Possible duplicate of [Order of the JSON objects, using jacksons ObjectMapper](http://stackoverflow.com/questions/19272830/order-of-the-json-objects-using-jacksons-objectmapper) – diziaq Nov 20 '15 at 05:50

4 Answers4

25

As others suggested, ordering should not matter. Nonetheless, if you prefer certain ordering, use @JsonPropertyOrder annotation like so:

@JsonPropertyOrder({ "x", "y", "x" })
public class EntityObj {
}
StaxMan
  • 113,358
  • 34
  • 211
  • 239
  • 2
    or can define this globally `objectMapper.configure(MapperFeature.SORT_PROPERTIES_ALPHABETICALLY, true)` – Nikita Koksharov Jan 03 '14 at 10:31
  • Ordering doesn't matter technically. But really matters for developers who try to debug or try to keep code and data clean and well organized. Best may be the field order and not to deviate from that. – Daniel Hári Jun 22 '23 at 14:54
  • @DanielHári alas, field order is not something one can rely on as JDK is free to give Field definitions in any which order it wants to -- it is not guaranteed to be source-code declaration order (and for later JDKs it isn't). – StaxMan Jul 06 '23 at 01:21
8

If alphabetical order suit you and you are using Spring Boot, you can add this in your application.properties :

spring.jackson.mapper.sort-properties-alphabetically=true
Victor Petit
  • 2,632
  • 19
  • 19
4

I realized this doesn't work with variable names that start with upper case letters. For example a variable named "ID" will not be ordered.

lochi
  • 860
  • 2
  • 12
  • 26
  • 1
    you can fix that by using @JsonPropertyOrder({"id","otherfields"}), the key is to list the fields out as they occur in the JSON output. – pushNpop Mar 25 '14 at 11:14
  • I can't +1 this enough. Jackson @JsonPropertyOrder requires every property in the class you want to be ordered to be camel cased. Have not seen that documented ANYWHERE. – John Smith Feb 11 '15 at 20:39
1

If you don't want to explicitly define the field order as done in the accepted answer, you can simply use:

@JsonPropertyOrder(alphabetic=true)