737

I'm using Jackson JSON library to convert some JSON objects to POJO classes on an android application. The problem is, the JSON objects might change and have new fields added while the application is published, but currently it will break even when a simple String field is added, which can safely be ignored.

Is there any way to tell Jackson to ignore newly added fields? (e.g. non-existing on the POJO objects)? A global ignore would be great.

mathk
  • 7,973
  • 6
  • 45
  • 74
Hadi Eskandari
  • 25,575
  • 8
  • 51
  • 65

12 Answers12

1023

Jackson provides an annotation that can be used on class level (JsonIgnoreProperties).

Add the following to the top of your class (not to individual methods):

@JsonIgnoreProperties(ignoreUnknown = true)
public class Foo {
    ...
}

Depending on the jackson version you are using you would have to use a different import in the current version it is:

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;

in older versions it has been:

import org.codehaus.jackson.annotate.JsonIgnoreProperties;
Andrei Sfat
  • 8,440
  • 5
  • 49
  • 69
Hadi Eskandari
  • 25,575
  • 8
  • 51
  • 65
  • Over time, I'd rather go with this option more often, as it is less intrusive. – Hadi Eskandari Mar 21 '13 at 02:21
  • 3
    Trying to get this working on Jackson 2.x libs. This package is now changed to `com.fasterxml.jackson.annotation.JsonIgnoreProperties` and it isn't working for me. Any thoughts? – Niks Mar 24 '14 at 13:14
  • 1
    Using this lib in my gradle file:'com.fasterxml.jackson.core:jackson-annotations:2.3.2' + com.fasterxml.jackson.annotation.JsonIgnoreProperties works for me with the jackson core lib. – Whitney Imura May 19 '14 at 16:26
  • 8
    how to do this with ObjectMapper? – blackuprise Jul 03 '14 at 13:23
  • 1
    It worked using the `com.fasterxml.jackson.annotation.JsonIgnoreProperties` import, as suggested by @NikhilPatil – Roger Oct 14 '14 at 19:10
  • 17
    Any way to do it on ObjectMapper level? I can't edit the class I am deserializing. Many people use it and they need those fields during deserialization but I don't. Those fields are known properties. – Ava Nov 14 '14 at 01:20
  • Spring Boot 1.4.1 uses the com.fasterxml.jackson.annotation package. – Darth Jon Oct 28 '16 at 12:39
  • Can we make it ignore properties by default instead of adding annotation to every class? – user1735921 Dec 15 '20 at 19:08
  • @Ava as of now, we can configure `ObjectMapper` to not fail for unknown propertie: `new ObjectMapper().configure(FAIL_ON_UNKNOWN_PROPERTIES, false);` Other configurable stuffs: https://fasterxml.github.io/jackson-databind/javadoc/2.6/com/fasterxml/jackson/databind/DeserializationFeature.html – hydradon Aug 30 '23 at 15:51
561

In addition to 2 mechanisms already mentioned, there is also global feature that can be used to suppress all failures caused by unknown (unmapped) properties:

// jackson 1.9 and before
objectMapper.configure(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES, false);
// or jackson 2.0
objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);

This is the default used in absence of annotations, and can be convenient fallback.

Anshul Goyal
  • 73,278
  • 37
  • 149
  • 186
StaxMan
  • 113,358
  • 34
  • 211
  • 239
  • This approach was required for me since the target POJO had a member of a type we do not own that had its own custom deserialization (which would use "hidden" properties such as "__type" to do certain types of magic such as instantiations of polymorphic types). Thanks! – scorpiodawg Oct 17 '13 at 16:53
  • 1
    This is for clients to umarshall it manually! – Marcello DeSales Jun 30 '14 at 18:59
  • 10
    Be aware that Spring >= 4.1.1 sets this to false automatically, without notice... https://jira.spring.io/browse/SPR-11891 – bigstones May 06 '16 at 09:16
  • 4
    why this config has no effet for me? jackson version 2.6.4 or 2.7.0 – zhaozhi Sep 02 '17 at 03:04
  • 2
    @zhaozhi These settings have been around for long time, so although you really should upgrade to latest patches for branch (2.6.7, 2.7.9), that's probably not reason. Most common reason is that framework you are using is not using `ObjectMapper` you are configuring. – StaxMan Sep 07 '17 at 02:44
  • 2
    I beg to differ but if the fields change name, this will silently fail and we have no idea what is happening unless we check field by field. In my case, it even **ignores the failure when the DTO lacks any field definition**, which is very dangerous. So it will ignore the failure no matter what type is; use with caution. – WesternGun Oct 02 '18 at 13:40
155

Up to date and complete answer with Jackson 2


Using Annotation

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;

@JsonIgnoreProperties(ignoreUnknown = true)
public class MyMappingClass {

}

See JsonIgnoreProperties on Jackson online documentation.

Using Configuration

Less intrusive than annotation.

import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.ObjectReader;

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

ObjectReader objectReader = objectMapper.reader(MyMappingClass.class);
MyMappingClass myMappingClass = objectReader.readValue(json);

See FAIL_ON_UNKNOWN_PROPERTIES on Jackson online documentation.

Prabhat
  • 338
  • 4
  • 20
Yves M.
  • 29,855
  • 23
  • 108
  • 144
  • 8
    this answer is very useful too in case one can't edit the MappingClass in order to add the annotation – Neuro May 30 '17 at 10:38
  • 2
    You can also configure the ObjectReader directly with `reader.without(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES)` – fono Nov 29 '17 at 08:56
  • Upvote. But adding a link for "with di.xml". (dependency injection .xm) : https://stackoverflow.com/questions/14343477/how-do-you-globally-set-jackson-to-ignore-unknown-properties-within-spring/14343479#14343479 – granadaCoder Nov 18 '21 at 22:20
94

it can be achieved 2 ways:

  1. Mark the POJO to ignore unknown properties

    @JsonIgnoreProperties(ignoreUnknown = true)
    
  2. Configure ObjectMapper that serializes/De-serializes the POJO/json as below:

    ObjectMapper mapper =new ObjectMapper();            
    // for Jackson version 1.X        
    mapper.configure(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES, false);
    // for Jackson version 2.X
    mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false) 
    
Amit Kaneria
  • 5,466
  • 2
  • 35
  • 38
34

@JsonIgnoreProperties(ignoreUnknown = true) worked well for me. I have a java application which runs on tomcat with jdk 1.7.

digital.aaron
  • 5,435
  • 2
  • 24
  • 43
Parul
  • 341
  • 3
  • 2
  • Worked for Spring Boot app with jdk 1.8 as well (none of step #2 from [this](https://stackoverflow.com/a/45743651/3794552) answer was needed) – Bimde Jun 26 '18 at 13:53
26

If using a pojo class based on JSON response. If chances are there that json changes frequently declare at pojo class level:

@JsonIgnoreProperties(ignoreUnknown = true)

and at the objectMapper add this if you are converting:

objectMapper.configure(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES, false);

So that code will not break.

Jeen Broekstra
  • 21,642
  • 4
  • 51
  • 73
user5636084
  • 261
  • 3
  • 2
19

Starting with Jackson version 2.4 and above there have been some changes. Here is how you do it now:

import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;

..........................................................................

 ObjectMapper mapper = new ObjectMapper();
    // to prevent exception when encountering unknown property:
 mapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);

Note: The @annotation based solution remains the same so if you like to use that see the other answers.

For more information see the 10 minutes Configuration tutorial at: https://github.com/FasterXML/jackson-databind

Alboz
  • 1,833
  • 20
  • 29
17

Make sure that you place the @JsonIgnoreProperties(ignoreUnknown = true) annotation to the parent POJO class which you want to populate as a result of parsing the JSON response and not the class where the conversion from JSON to Java Object is taking place.

mana
  • 6,347
  • 6
  • 50
  • 70
Rushi Shah
  • 2,031
  • 2
  • 14
  • 10
11

As stated above the annotations only works if this is specified in the parent POJO class and not the class where the conversion from JSON to Java Object is taking place.

The other alternative without touching the parent class and causing disruptions is to implement your own mapper config only for the mapper methods you need for this.

Also the package of the Deserialization feature has been moved. DeserializationConfig.FAIL_ON_UNKNOWN_PROPERTIES to DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES

import org.codehaus.jackson.map.DeserializationConfig;
...
objectMapper.configure(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES, false);
mekdev
  • 468
  • 6
  • 14
10

For whom are using Spring Boot, you can configure the default behaviour of Jackson by using the Jackson2ObjectMapperBuilder.

For example :

@Bean
public Jackson2ObjectMapperBuilder configureObjectMapper() {
    Jackson2ObjectMapperBuilder oMapper = new Jackson2ObjectMapperBuilder();
    oMapper.failOnUnknownProperties(false);
    return oMapper;
}

Then you can autowire the ObjectMapper everywhere you need it (by default, this object mapper will also be used for http content conversion).

Thoomas
  • 2,150
  • 2
  • 19
  • 33
8

I'm using jackson-xxx 2.8.5.Maven Dependency like:

<dependencies>
    <!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-core -->
    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-core</artifactId>
        <version>2.8.5</version>
    </dependency>

    <!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-annotations -->
    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-annotations</artifactId>
        <version>2.8.5</version>
    </dependency>

    <!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind -->
    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-databind</artifactId>
        <version>2.8.5</version>
    </dependency>

</dependencies>

First,If you want ignore unknown properties globally.you can config ObjectMapper.
Like below:

ObjectMapper objectMapper = new ObjectMapper();

objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);

If you want ignore some class,you can add annotation @JsonIgnoreProperties(ignoreUnknown = true) on your class like:

@JsonIgnoreProperties(ignoreUnknown = true)
public class E1 {

    private String t1;

    public String getT1() {
        return t1;
    }

    public void setT1(String t1) {
        this.t1 = t1;
    }
}
inOut
  • 1,123
  • 1
  • 9
  • 15
-1

You can annotate the specific property in your POJO with @JsonIgnore.

Prasanna
  • 3,703
  • 9
  • 46
  • 74
  • 9
    The situation is the other way around, e.g. converting JSON to POJO, so it is not possible to annotate the property (which does not exists on the POJO) – Hadi Eskandari Mar 29 '11 at 09:12
  • 1
    This does not answer the question, as Hadi said, there is no field to annotate in POJO – thermz Apr 10 '13 at 09:05
  • This will actually completely abstain Jackson for (de)marshalling this property. So even if the property will appear in you model it will not be taken into 'account' when (de)marshalling via Jackson – maryoush Oct 05 '16 at 07:42