Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Model class for Jackson Parser to parse Uppercase properties

I have a json file which looks like this:

{
    "ANIMALS": {
    "TYPE": "MAMMAL",
    "COLOR": "BLACK",
    "HEIGHT": "45",

    }
}

But I get property not found error. If I change it to animals(lowercase). it works fine. Can anyone suggest me the model class for this sample json file which will get parsed correctly.

like image 887
Deepak Senapati Avatar asked May 22 '13 05:05

Deepak Senapati


People also ask

How to parse JSON object in Java using Jackson?

Jackson JSON - Edit JSON Document readAllBytes(Paths. get("employee. txt")); ObjectMapper objectMapper = new ObjectMapper(); //create JsonNode JsonNode rootNode = objectMapper. readTree(jsonData); //update JSON data ((ObjectNode) rootNode).

What is Jackson deserializer?

Jackson is a powerful and efficient Java library that handles the serialization and deserialization of Java objects and their JSON representations. It's one of the most widely used libraries for this task, and runs under the hood of many other frameworks.

How does ObjectMapper work?

The way ObjectMapper works to figure out which JSON field maps to which POJO field is by matching the names of the JSON fields to the names of the getter and setter methods in the POJO.

Which constructor does Jackson use?

Jackson Deserialization Using Lombok Builders This class is also immutable and it has a private constructor. Hence, we can create instances only through its builder. This is enough to use this class for deserialization with Jackson. Also notice that Lombok uses build as the default name of the build method.


1 Answers

Building off of Deepak's answer, depending on how you have Jackson configured, you may need to put the @JsonProperty on the getters & setters instead of the property or you might get duplicate properties in the resulting JSON.

Example

 @JsonProperty("ANIMALS")
 private string animals;

Results in...{animals:"foo",ANIMALS:"foo"}

 private string animals;

 @JsonProperty("ANIMALS")
 public String getAnimals(){...}

Results in...{ANIMALS:"foo"}

like image 99
Snekse Avatar answered Oct 07 '22 17:10

Snekse