0

I have the following requirement for JSON string conversion to Java Object.

class Person {
     private String firstName;
     private String lastName;
}
ObjectMapper MAPPER = new ObjectMapper();
String jsonString = "{\"FST_NME\":\"stack\",\"LST_NME\":\"OVERFLOW\"}";
Person person = MAPPER.readValue(jsonString, Person.class);

The above conversion returns null as the Person class attribute name doesn't match.

With @JsonProperty it converts correctly, but the final JSON result key is the same key as in jsonString.

{
   "FST_NME" : "stack",
   "LST_NME" : "overflow"
}

but I am looking for something like below.

{
   "firstName" : "stack",
   "lastName" : "overflow"
}

I tried renaming the key in jsonString and it works as expected.

But can we achieve the above result using any annotations or any other approach?

Thanks.

saravanan
  • 151
  • 2
  • 15

3 Answers3

1

Add below annotation on gets methods.

@JsonGetter("FST_NME")
public String getFirstName(){
return first Name;
}
S. Anushan
  • 728
  • 1
  • 6
  • 12
1

You just need to add @JsonProperty in both setter and getters.

In your case, You are reading JSON string key FST_NME, so you need to add @JsonProperty('FST_NME') in the setter method for firstName and as you want to get the final JSON string with key firstName so you need to add @JsonProperty('firstName') in the getter method of firstName. And same for lastName.

Following is the working code.

package com.ubaid.stackoverflow;

import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;

@Slf4j
public class Saravanan {

    @SneakyThrows
    public static void main(String[] args) {

        ObjectMapper MAPPER = new ObjectMapper();
        String jsonString = "{\"FST_NME\":\"stack\",\"LST_NME\":\"OVERFLOW\"}";
        Person person = MAPPER.readValue(jsonString, Person.class);
        String finalJson = MAPPER.writeValueAsString(person);
        log.debug("Final JSON: {}", finalJson);
    }
}

class Person {
    private String firstName;
    private String lastName;

    @JsonProperty("firstName")
    public String getFirstName() {
        return firstName;
    }

    @JsonProperty("FST_NME")
    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    @JsonProperty("lastName")
    public String getLastName() {
        return lastName;
    }

    @JsonProperty("LST_NME")
    public void setLastName(String lastName) {
        this.lastName = lastName;
    }
}

The output of above code is:

Final JSON: {"firstName":"stack","lastName":"OVERFLOW"}
Ubaid ur Rehman
  • 128
  • 1
  • 7
0
  1. Read data to a DTO class (Person DTO) with @JsonProperty
  2. Class Person as you wish
  3. Convert DTO to Person
class PersonDTO {
     @JsonProperty(value = "FST_NME")
     private String firstName;
     @JsonProperty(value = "LST_NME")
     private String lastName;
}
class Person {
     private String firstName;
     private String lastName;
}


ObjectMapper MAPPER = new ObjectMapper();
String jsonString = "{\"FST_NME\":\"stack\",\"LST_NME\":\"OVERFLOW\"}";
PersonDTO persondto = MAPPER.readValue(jsonString, PersonDTO.class);

Person person = new Person();
person.setFirstName(persondto.getFirstName());
person.setLastName(persondto.getLastName());