221

I'm using jackson to convert an object of mine to json. The object has 2 fields:

@Entity
public class City {
   @id
   Long id;
   String name;
   public String getName() { return name; }
   public void setName(String name){ this.name = name; }
   public Long getId() { return id; }
   public void setName(Long id){ this.id = id; }
}

Since I want to use this with the jQuery auto complete feature I want 'id' to appear as 'value' in the json and 'name' to appear as 'label'. The documentation of jackson is not clear on this and I've tried every annotation that even remotely seems like it does what I need but I can't get name to appear as label and id to appear as value in the json.

Does anyone know how to do this or if this is possible?

1
  • if If you were ever going to potentially use the entity ("City") for any other purpose or business logic, I'd avoid defining its JSON representation as Label & Value. OTOH if this is purely reference data used for combos, you're likely going to have more -- making a generic entity (Pair or Option) to cover all of these is a possible approach to consider.
    – Thomas W
    Commented Jan 16, 2018 at 1:17

4 Answers 4

393

Have you tried using @JsonProperty?

@Entity
public class City {
   @id
   Long id;
   String name;

   @JsonProperty("label")
   public String getName() { return name; }

   public void setName(String name){ this.name = name; }

   @JsonProperty("value")
   public Long getId() { return id; }

   public void setId(Long id){ this.id = id; }
}
5
  • 8
    Yes, I had tried that, however I was doing @JsonProperty(value="label") and it was not working, I've tried it as you have suggested and it works! thanks man this will really help simplify the code now.
    – Ali
    Commented Sep 1, 2011 at 16:28
  • I have a similar requirement. Just that I'm using Jackson with RESTEasy and this doesn't work :-/ Strange!
    – Niks
    Commented Mar 19, 2014 at 13:18
  • verify that Jackson really is the configured provider and not something else Commented Mar 19, 2014 at 15:23
  • 2
    is there a way to leverage Jackson (or GSON) to change a property name in an already-existing JSON string (not during serialization)? Commented Oct 6, 2015 at 13:44
  • I have used object @JsonProperty("object") val jsonObject: String, ...but failed Commented Jul 27, 2018 at 12:52
59

Be aware that there is org.codehaus.jackson.annotate.JsonProperty in Jackson 1.x and com.fasterxml.jackson.annotation.JsonProperty in Jackson 2.x. Check which ObjectMapper you are using (from which version), and make sure you use the proper annotation.

1
  • There is also a org.json4s.jackson namespace. :) and possibly other implementation of jackson YMMV. Commented May 14, 2018 at 21:17
33

Jackson

If you are using Jackson, then you can use the @JsonProperty annotation to customize the name of a given JSON property.

Therefore, you just have to annotate the entity fields with the @JsonProperty annotation and provide a custom JSON property name, like this:

@Entity
public class City {

   @Id
   @JsonProperty("value")
   private Long id;

   @JsonProperty("label")
   private String name;

   //Getters and setters omitted for brevity
}

JavaEE or JakartaEE JSON-B

JSON-B is the standard binding layer for converting Java objects to and from JSON. If you are using JSON-B, then you can override the JSON property name via the @JsonbProperty annotation:

@Entity
public class City {

   @Id
   @JsonbProperty("value")
   private Long id;

   @JsonbProperty("label")
   private String name;

   //Getters and setters omitted for brevity
}
22

There is one more option to rename field:

Jackson MixIns.

Useful if you deal with third party classes, which you are not able to annotate, or you just do not want to pollute the class with Jackson specific annotations.

The Jackson documentation for Mixins is outdated, so this example can provide more clarity. In essence: you create mixin class which does the serialization in the way you want. Then register it to the ObjectMapper:

objectMapper.addMixIn(ThirdParty.class, MyMixIn.class);
1
  • 1
    I am curious will this affect the performance when serializing/deserializing fields?
    – chanllen
    Commented Sep 10, 2019 at 23:38

Not the answer you're looking for? Browse other questions tagged or ask your own question.