skip to Main Content

I have json

{
    "Person": {
        "PersonId": 122,
        "Name": "John",
        "Surname": "Doe",
        "age": 2
    }
}

I want to convert it to Java class

    public class Person {
        private String personId;
        private String name;
        private String surname;
        private int age;
        
        //getters,setters...
    }

And I don’t have permission to change this Person class.

What I already have tried is


    @Bean
    ObjectMapper objectMapper() {
        ObjectMapper mapper = new ObjectMapper();
        mapper.setPropertyNamingStrategy(new LowerCaseStrategy());
        return mapper;
    }

    public static class LowerCaseStrategy extends PropertyNamingStrategy.PropertyNamingStrategyBase {
        @Override
        public String translate(String input) {
            char[] chars = input.toCharArray();
            chars[0] = Character.toLowerCase(chars[0]);
            return new String(chars);
        }
    }

But it did not help.

Does anyone have an idea how this can be solved?

2

Answers


  1. To convert the JSON with uppercased field names to the desired Java class, you can use the @JsonProperty annotation from Jackson library. Here’s how you can modify your Person class and ObjectMapper configuration to achieve the conversion:

    1. Modify the Person class to use the @JsonProperty annotation:
    public class Person {
        @JsonProperty("PersonId")
        private String personId;
        
        @JsonProperty("Name")
        private String name;
        
        @JsonProperty("Surname")
        private String surname;
        
        @JsonProperty("age")
        private int age;
    
        // Getters and setters...
    }
    
    1. Remove the custom LowerCaseStrategy from your ObjectMapper configuration since the field names in your JSON are already in lowercase.
    @Bean
    public ObjectMapper objectMapper() {
        return new ObjectMapper();
    }
    

    Now, when you deserialize the JSON using the updated ObjectMapper, it will correctly map the fields with uppercased names to the corresponding fields in the Person class.

    For example:

    String jsonString = "{"Person":{"PersonId":122,"Name":"John","Surname":"Doe","age":2}}";
    
    ObjectMapper objectMapper = new ObjectMapper();
    Person person = objectMapper.readValue(jsonString, Person.class);
    
    System.out.println(person.getPersonId()); // Output: 122
    System.out.println(person.getName()); // Output: John
    System.out.println(person.getSurname()); // Output: Doe
    System.out.println(person.getAge()); // Output: 2
    

    With these changes, the JSON with uppercased field names will be correctly converted to the Java Person object.

    Login or Signup to reply.
  2. You are almost there. The one error you made, is the direction in which the PropertyNamingStrategy applies: You are converting from Java name, to JSON name, not the other way around. So, this should work:

            @Override
            public String translate(String input) {
                char[] chars = input.toCharArray();
                chars[0] = Character.toUpperCase(chars[0]); // upper!
                return new String(chars);
            }
    

    Also, Jackson has PropertyNamingStrategies.UPPER_CAMEL_CASE which should do the thing you need without implementing your own.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search