Search code examples
serializationjacksonjackson-databind

How do I write JSON using Jackson ObjectMapper without underscores in the json keys


I have some json which have underscores in the names which come from aa 3rd party api so I am not able to change them. However, I want to use bean property names without the underscore. Here is a sample of JSON that represents one of the objects. This is JSON which I want to map to a POJO called Magazine. However, when I convert to Magazine object back to JSON the underscores appear in the JSON. How can I get the output without the underscore names? I want to be able to use the bean names without underscores, like name instead of _name in the POJO.


                    {
                        "_name": "Vogue",
                        "authors": ["Jenny Mason", "Craig Hall"],
                        "_edition": "plastic-surgery",
                        "_unit_price": "!.75",
                        "_publication_date": "03/22/2023"
                    }

Here is the Jackson object mapper configuration:


              public ObjectMapper initMapper(){
                 ObjectMapper mapper = new ObjectMapper();
                 mapper.registerModule(new JavaTimeModule());
                 mapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
                 mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
                 mapper.setSerializationInclusion(JsonInclude.Include.NON_EMPTY);
                 mapper.enable(SerializationFeature.INDENT_OUTPUT);
                 return mapper;
              }

Here is the object Magazine object to map JSON to:


                import com.fasterxml.jackson.annotation.JsonProperty;
                import lombok.Data;

                import java.util.List;

                @Data
                public class Magazine {
                  
                   @JsonProperty("name")
                   private String _name;
                  
                   @JsonProperty("authors")
                   private List<String> _authors;
                   
                   @JsonProperty("unitPrice")
                   private String _unitPrice;
                   
                   @JsonProperty("publicationDate")
                   private String _publication_date;
                   
                   @JsonProperty("edition")
                   private String _edition;

                   public Magazine(String _name, List<String> _authors, String _edition, 
                        String _unitPrice, String _publication_date) {
                
                     this._name = _name;
                     this._authors = _authors;
                     this._edition = _edition;
                     this._unitPrice = _unitPrice;
                     this._publication_date = _publication_date;
                   }
    
                 }



Solution

  • I was able to solve this using @JsonSetter and @JsonGetter. For example

       @JsonSetter("_name")
       public void setName(String name){
         this.name = name;
       }
    
       @JsonGetter("name")
       public String getName(){
         return name;
       }