0

I have a JSON file with multiple entries that have same attribute names, but different attribute values, such as:

{
  "name" : { "first" : "A", "last" : "B" },
  "gender" : "MALE",
  "married" : false,
  "noOfChildren" : 2
},
{
  "name" : { "first" : "C", "last" : "D" },
  "gender" : "FEMALE",
  "married" : true,
  "noOfChildren" : 1
}

The class that it should be mapped is:

public class Human {

private Name name;
private String gender;
private int age;

<getter, setters etc>

}

EDIT: Service code is :

List<Human> humans = null;
ObjectMapper objectMapper = new ObjectMapper();
        objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);

ObjectMapper objectMapper = new ObjectMapper();
objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);

try {

    humans= objectMapper.readValue(json, new TypeReference<List<Human>>(){});

} catch (IOException e) {
    e.printStackTrace();
}

JSON is parsed from HTTP entity and with correct format and now I added the annotation ass suggested in the answers.

As you can see, they have some attributes in common, but differ in others, and I would like to map those common fields. Is it possible to map the JSON this way ? I have tried mapping JSON to a collection/list/array of JsonNodes, but I keep getting erros about deserialization, while mapping only one instance of JSON entry works just fine. Is there another way of doing this ?

2 Answers 2

1

Use

@JsonIgnoreProperties(ignoreUnknown = true)
public class Human {
    private Name name;
    private String gender;
    // getters, settets, default constructor
}

Or if you are using Lombok then it will be

@Data
@NoArgsConstructor
@JsonIgnoreProperties(ignoreUnknown = true)
public class Human {
    private Name name;
    private String gender;
}
Sign up to request clarification or add additional context in comments.

2 Comments

I don't want to ignore the whole class, only certain fields. But what about the attributes that aren't consisted in the class, such as noOfChildren or married ?
These attributes will be ignored, only name and gender will be in your object.
1

use

objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);

while deserializing json to POJO class.

The JSON you have provide in question will give following error, as it is not a valid one.

Can not deserialize instance of java.util.ArrayList out of START_OBJECT token

Valid Json would be like this:

[
  {
    "name": {
      "first": "A",
      "last": "B"
    },
    "gender": "MALE",
    "married": false,
    "noOfChildren": 2
  },
  {
    "name": {
      "first": "C",
      "last": "D"
    },
    "gender": "FEMALE",
    "married": true,
    "noOfChildren": 1
  }
]

6 Comments

It works for one instance of JSON, but if I have store multiple entries in an array of POJOs, i get something like this : Can not deserialize instance of java.util.ArrayList out of START_OBJECT token
can you show me sample code and json, which you are trying?
@borgmater, difference between curent and my answer is that when we set DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES to false for ObjectMapper object then this mapper will ignore unknown properties for any parsed object. In my answer I set to ignore only for class Human, i.e. it will ignore unknown properties for any ObjectMapper which will be used.
please also post exact json. one you have provided in question is not a valid one.
I have updated my answer, I think JSON format was the issue.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.