Order doesn't usually matter in JSON objects. But sometimes it does.

And when it does, Jackson gives you full control over how you order the properties.

In this guide, I'll show you how to tell Jackson to order properties the way you want them to appear after serialization.

Ready? Let's get started.

Why Would You Want to Do It?

I mentioned above that order doesn't usually matter in JSON objects. So why would you even consider ordering the properties?

One good reason is because it makes the output more readable. That's especially true with large objects.

If you're handling JSON objects for an ecommerce site, for example, you might have dozens of properties. 

(Take a look at Amazon's API if you don't believe me.)

During the QA phase, you might need to find specific properties and check their values. That's much easier to do when you (or the QA analyst) know where those properties are located.

And it's much easier to find specific properties when they're consistently ordered.

But you don't necessarily want to rearrange your whole Spring Boot Java class to just to produce an ordered output. 

Fortunately, Jackson offers a solution.

A Beautiful Annotation

It's as easy as using a simple annotation: @JsonPropertyOrder.

Of course, you're going to need to do a little more than provide an annotation. You'll need to also describe the order you're looking for.

You'll do that in parentheses next to the annotation.

Let's take a look at an example.

Here's an Employee class with properties that give us an employee's first name, last name, and ID.

public class Employee {

    private String lastName;
    private String firstName;
    private String id;    
    
    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String toString() {
        return ReflectionToStringBuilder.toString(this);
    }
}

As you can see, that's just a standard POJO with no special annotations.

Now write some code to serialize an instance of that object:

ObjectMapper mapper = new ObjectMapper();            

Employee employee = new Employee();
employee.setId("A17");
employee.setFirstName("Frank");
employee.setLastName("Smith");

System.err.println(mapper.writerWithDefaultPrettyPrinter().writeValueAsString(employee));

And you're going to get a result that looks like this:

{
  "lastName" : "Smith",
  "firstName" : "Frank",
  "id" : "A17"
}

The order of the properties in that JSON object match the order of the properties in the Java class:

  1. lastName
  2. firstName
  3. id

But that might not be what you're looking for.

Reordering

Let's do that same thing but this time we'll make use of the aforementioned @JsonPropertyOrder annotation.

Update the Employee class:

@JsonPropertyOrder({"id", "lastName", "firstName"})
public class Employee {

    private String lastName;
    private String firstName;
    private String id;    
    
    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String toString() {
        return ReflectionToStringBuilder.toString(this);
    }
}

Now, sitting atop the class declaration, there's a @JsonPropertyOrder annotation. But it's not alone.

There's a set of parentheses next to the annotation. And inside that set of parentheses there's a set of braces.

And inside the set of braces is where the order gets defined.

In the example above, the new order is:

  1. id
  2. lastName
  3. firstName

So that's the order you expect to see in the JSON object.

But will it work? One way to find out. Run this code again:

ObjectMapper mapper = new ObjectMapper();            

Employee employee = new Employee();
employee.setId("A17");
employee.setFirstName("Frank");
employee.setLastName("Smith");

System.err.println(mapper.writerWithDefaultPrettyPrinter().writeValueAsString(employee));

And this time you'll see this output:

{
  "id" : "A17",
  "lastName" : "Smith",
  "firstName" : "Frank"
}

Excellent. That's exactly what you're looking for.

Wrapping It Up

Now you know how to order properties with Jackson during the serialization process.

Feel free to take what you've learned here and put it in your own Spring Boot applications.

Have fun!

Micheile Henderson on Unsplash