In this short article, you'll learn how to ignore unknown JSON properties while parsing JSON to Java Object using Jackson. It is the most common issue while parsing JSON data using Jackson API.
Default Jackson Behavior
By default, Jackson API throws an exception if the Java class doesn't contain all the fields corresponding to all JSON properties.
For example, say we have the following JSON object:
{
"name": "John Doe",
"email": "john.doe@example.com",
"roles": [
"Member",
"Admin"
],
"admin": true,
"age": 32
}
Now we want to deserialize the above JSON object to the following Java class named User
:
public class User {
public String name;
public String email;
private String[] roles;
private boolean admin;
public User() {
}
public User(String name, String email, String[] roles, boolean admin) {
this.name = name;
this.email = email;
this.roles = roles;
this.admin = admin;
}
// getters and setters, toString() .... (omitted for brevity)
}
As you can see above, the User
class doesn't contain any field that matches the age
JSON property. Trying to parse the above JSON with an unknown property to a User
object will result in a UnrecognizedPropertyException
exception:
try {
// JSON string
String json = "{\"name\":\"John Doe\",\"email\":\"john.doe@example.com\"," +
"\"roles\":[\"Member\",\"Admin\"],\"admin\":true,\"age\":32}";
// convert JSON string to Java Object
User user = new ObjectMapper().readValue(json, User.class);
// print user
System.out.println(user);
} catch (Exception ex) {
ex.printStackTrace();
}
The above code will fail with the following exception:
com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: Unrecognized field "age" (class com.attacomsian.User), not marked as ignorable (4 known properties: "name", "email", "roles", "admin"])
To execute the above code successfully, we need to explicitly ignore unknown properties either on the class level or globally by configuring ObjectMapper
.
Ignore Unknown JSON Properties on Class Level
To ignore all unknown properties for a single class, place the @JsonIgnoreProperties
annotation on top of the class declaration, as shown below:
@JsonIgnoreProperties(ignoreUnknown = true)
public class User {
// ...
}
Now, if you execute the above code, you should see the following output without any exception:
User{name='John Doe', email='john.doe@example.com', roles=[Member, Admin], admin=true}
Ignore Unknown JSON Properties Globally
Another way of dealing with unknown properties while parsing JSON is to configure the ObjectMapper
class not to fail when it encounters an unfamiliar property:
try {
// JSON string
String json = "{\"name\":\"John Doe\",\"email\":\"john.doe@example.com\"," +
"\"roles\":[\"Member\",\"Admin\"],\"admin\":true,\"age\":32}";
// create object mapper instance
ObjectMapper mapper = new ObjectMapper();
// ignore unknown properties
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
// convert JSON string to Java Object
User user = mapper.readValue(json, User.class);
// print user
System.out.println(user);
} catch (Exception ex) {
ex.printStackTrace();
}
For more Jackson examples, check out the How to read and write JSON using Jackson in Java tutorial.
✌️ Like this article? Follow me on Twitter and LinkedIn. You can also subscribe to RSS Feed.