# How to ignore unknown JSON properties with Jackson

By Atta Ur Rehman Shah (https://attacomsian.com/about). Published Nov 17, 2019, updated Oct 14, 2022. Topics: Java, JSON, Jackson.
Canonical URL: https://attacomsian.com/blog/jackson-ignore-unknown-json-properties

> A short tutorial to learn how to ignore unknown JSON properties while parsing JSON to a Java Object using Jackson API.

In this short article, you'll learn how to ignore unknown JSON properties while [parsing JSON to Java Object](https://attacomsian.com/blog/jackson-convert-java-object-to-from-json) using [Jackson](https://attacomsian.com/blog/jackson-read-write-json). 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:

```json
{
  "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`:

```java
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:

```java
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:

```bash
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:

```java
@JsonIgnoreProperties(ignoreUnknown = true)
public class User {
    // ...
}
```

Now, if you execute the above code, you should see the following output without any exception:

```bash
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:

```java
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](https://attacomsian.com/blog/jackson-read-write-json) tutorial.
