Explain Codes LogoExplain Codes Logo

How to change a field name in JSON using Jackson

java
json-engineering
objectmapper
field-renaming
Anton ShumikhinbyAnton Shumikhin·Sep 23, 2024
TLDR

Using @JsonProperty changes JSON field names in Jackson. Apply as shown:

class Example { @JsonProperty("newFieldName") //Flo's Diner? Nah mate, Renamed Resto! private String oldFieldName; //"Old field name" old but gold. }

When serializing an Example object, it will output JSON with "newFieldName" instead of "oldFieldName".

Pro tips for field renaming with Jackson

Keep getters consistent

Annotating fields directly can be straightforward, but applying @JsonProperty on getter methods can ensure serialization and deserialization consistency.

class Example { private String oldFieldName; @JsonProperty("newFieldName") //Tony Stark? Nah, Iron Man! public String getOldFieldName() { return oldFieldName; } }

JPA and your class

If you're using an ORM framework like Hibernate, don't forget to annotate your class with @Entity. It does not directly influence Jackson's output but preps your class for JPA context.

Setter inclusion

For deserialization, ensure to provide setter methods for renamed fields. This way, Jackson can accurately reconstruct your class instances during the JSON-to-Java transformation.

Unmodifiable class? Use MixIns

Sometimes, changing the original class isn’t feasible. When that happens, Jackson MixIns are your best mates, allowing you to dictate field names for serialization without touching the class itself.

@JsonMixInAnnotations( value = @JsonProperty("newFieldName"), //DJ's name change: "oldFieldName" -> "newFieldName" mixin = Example.class ) interface ExampleMixIn {} ObjectMapper mapper = new ObjectMapper(); mapper.addMixIn(Example.class, ExampleMixIn.class);

Library and version compatibility

Double-check your ObjectMapper version to ensure you're using compatible annotations. For Java EE environment users, consider using @JsonbProperty from the Jakarta JSON Binding API as a @JsonProperty alternative.

Visualising field renaming

JSON before field renaming

Let's consider the JSON representation of a book:

Original JSON: {"title": "1984", "author": "George Orwell"}

JSON after field renaming with Jackson

Imagine using @JsonProperty to change a book's identity:

class Book { @JsonProperty("book_title") // "title" is so mainstream. private String title; @JsonProperty("book_author") // Because "author" is too bland. private String author; }

Your book now sports a fresh look:

Altered JSON: {"book_title": "1984", "book_author": "George Orwell"}

More Jackson renaming magic

Try changing the attributes of a character within a novel:

Profile Before (JSON): {"firstName": "Winston", "lastName": "Smith"} Jackson's Wand (Not from Hogwarts): @JsonProperty("forename") & @JsonProperty("surname")

After a bit of magic:

Profile After (JSON): {"forename": "Winston", "surname": "Smith"}

Now the novel's character profile reads like a rye British file. 📖✨

Mastering field renaming

Polymorphism and inheritance

Handling inheritance challenges? Overriding field getters is possible with @JsonProperty. It supports renaming per class type, embracing the polymorphic nature of your objects.

Field order matters

For ordered JSON fields, Jackson provides @JsonPropertyOrder. This lets you dictate your fields' sequencing. No more messy closets!

Conditional field inclusion

Utilize @JsonInclude to exclude undesired or null fields, because sometimes, it's not about showing off all your teeth.