Jackson Json Pojo Parsing Error - HTML
Jackson Json Pojo Parsing Error - HTML
html
While parsing JSON string received from one of our RESTful web services, I was getting this error “Exception in thread “main”
com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: Unrecognized field “person” (class Hello$Person), not marked as
ignorable”.
After some research, I found that this is one of the common error while parsing JSON document using Jackson open source library in Java
application. The error messages say that it is not able to find a suitable property name called “person” in our case, let’s first take a look at the JSON
we are trying to parse, the class we are using to represent the JSON document and the error message itself.
Error Message:
The error messages say that it can find out id, city, name and phone attributes in the Person class but not able to locate the “person” field.
01 class Person{
02
03 private int id;
04
05 private String name;
06
07 private String city;
08
09 private long phone;
10
11 .....
12
13 }
01 {
02
03 "person": [
04
05 {
06
07 "id": "11",
08
09 "name": "John",
10
11 "city": "NewYork",
12
13 "phone": 7647388372
14
15 }
16
17 ]
18
19 }
If you look carefully, the “person” field points to a JSON array and not object, which means it cannot be mapped to person class directly.
Now, the error will go away but the Output is not what you expected, it will print following:
You can see that Person class is not created properly, the relevant attributes are null even though the JSON String contains its value.
The reason was that JSON String contains a JSON array, the person field is pointing towards an array and there is no field corresponding to that in
Person class.
In order to properly parse the JSON String we need to create a wrapper class Community which will have an attribute to keep an array of Person as
shown below:
Now, we will convert the JSON String to this Community class and print each person from the list as shown below:
Now, coming back to a more general situation where a new field is added on JSON but not available in your Person class, let’s see what happens.
01 {
02
03 "person": [
04
05 {
06
07 "id": "11",
08
09 "name": "John",
10
11 "city": "NewYork",
12
13 "phone": 7647388372,
14
15 "facebook": "JohnTheGreat"
16
17 }
18
19 ]
20
21 }
When you run the same program with this JSON String, you will get following error:
Again, Jackson is not able to recognize the new “facebook” property. Now, we can ignore this property by disabling the feature which tells Jackson to
fail on the unknown property as shown below:
4
5 Community c = objectMapper.readValue(JSON, Community.class);
And this will print the person class properly as shown below:
Alternatively, you can also use @JsonIgnoreProperties annotation to ignore undeclared properties.
The @JsonIgnoreProperties is a class-level annotation in Jackson and it will ignore every property you haven’t defined in your POJO. Very useful
when you are just looking for a couple of properties in the JSON and don’t want to write the whole mapping.
This annotation provides control at class level i.e. you can tell Jackson that for this class, please ignore any attribute not defined by doing
1 @JsonIgnoreProperties(ignoreUnknown = true)
01 @JsonIgnoreProperties(ignoreUnknown = true)
02
03 static class Person{
04
05 private int id;
06
07 private String name;
08
09 private String city;
10
11 private long phone;
12
13 ......
14
15 }
Sample program
008
009 /*
010 * {
011 "person": [
012 {
013 "id": "11",
014 "name": "John",
015 "city": "NewYork",
016 "phone": 7647388372
017 }
018 ]
019 }
020 */
021
022 public class Hello {
023
024 private static String JSON = "{\r\n" + " \"person\": [\r\n" + " {\r\n"
025 + " \"id\": \"11\",\r\n" + " \"name\": \"John\",\r\n"
026 + " \"city\": \"NewYork\",\r\n" + " \"phone\": 7647388372,\r\n"
027 + " \"facebook\": \"JohnTheGreat\"\r\n" + " }\r\n" + " ]\r\n" + " } ";
028
029 public static void main(String args[]) throws JsonParseException,
030 JsonMappingException, IOException {
031
032 ObjectMapper objectMapper = new ObjectMapper();
033 objectMapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
034 Community c = objectMapper.readValue(JSON, Community.class);
035
036 for (Person p : c.getPerson()) {
037 System.out.println(p);
038 }
039
040 }
041
042 static class Community {
043 private List<Person> person;
044
045 public List<Person> getPerson() {
046 return person;
047 }
048
049 public void setPerson(List<Person> person) {
050 this.person = person;
file:///D:/Downloads/Jackson JSON-Pojo Parsing Error.html 6/11
13/10/21 23:27 Jackson JSON-Pojo Parsing Error.html
051 }
052
053 }
054
055 static class Person {
056 private int id;
057 private String name;
058 private String city;
059 private long phone;
060
061 public int getId() {
062 return id;
063 }
064
065 public void setId(int id) {
066 this.id = id;
067 }
068
069 public String getName() {
070 return name;
071 }
072
073 public void setName(String name) {
074 this.name = name;
075 }
076
077 public String getCity() {
078 return city;
079 }
080
081 public void setCity(String city) {
082 this.city = city;
083 }
084
085 public long getPhone() {
086 return phone;
087 }
088
089 public void setPhone(long phone) {
090 this.phone = phone;
091 }
092
093 @Override
file:///D:/Downloads/Jackson JSON-Pojo Parsing Error.html 7/11
13/10/21 23:27 Jackson JSON-Pojo Parsing Error.html
When I run first version of this program, I was greeted with the following error:
This error was occurring because my nested class Person was not static, which means it cannot be instantiated because having any Outer class
instance. The issue resolved after making the Person class static.
If you are not familiar with this detail before, I suggest you check
Java Fundamentals: The Core Platform, a free course from Pluralsight to learn more about such details of Java programming language. You can
signup for a free trial, which gives you 10 days access, enough to learn whole Java for free.
06
07 at com.fasterxml.jackson.databind.DeserializationContext.reportUnknownProperty(DeserializationContext.j
08
09 at com.fasterxml.jackson.databind.deser.std.StdDeserializer.handleUnknownProperty(StdDeserializer.java:
10
11 at com.fasterxml.jackson.databind.deser.BeanDeserializerBase.handleUnknownProperty(BeanDeserializerBase
12
13 at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserializeFromObject(BeanDeserializer.java:31
14
15 at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:121)
16
17 at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:2888)
18
19 at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:2034)
20
21 at Hello.main(Hello.java:40)
When you run the final version of the program you will see following output:
This means we are able to parse JSON containing unknown attributes successfully in Jackson.
In Eclipse, you don’t even need to create the class file because it will automatically create the class and package if you copy paste the code in Java
project.
If Eclipse is your primary IDE and you want to learn more of such productivity tips I suggest you check out
The Eclipse Guided Tour – Part 1 and 2 By Tod Gentille.
It’s a free, online course to learn both basic and advanced feature of Eclipse IDE, which every Java developer should be aware of. You can get access
to this course by signing up for a free trial, which gives you 10 days access to the whole Pluralsight library, one of the most valuable collection to
learn about programming and other technology. Btw, 10 days is more than enough to learn Java and Eclipse together.
Anyway, once you copy paste the code, all you need to do is either include Maven dependency in your pom.xml or manually download required JAR
file for Jackson open source library.
You can add following Maven dependency on your project’s pom.xml and then run the mvn build or mvn install command to compile:
1 <dependency>
2 <groupId>com.fasterxml.jackson.core</groupId>
3 <artifactId>jackson-databind</artifactId>
4 <version>2.2.3</version>
5 </dependency>
This dependency requires jackson-core and jackson-annotations but Maven will automatically download that for you.
If you are not using Maven or any other build tool e.g.gradle then you can just go to Maven central library and download following three JAR files
and include them in your classpath:
1 jackson-databind-2.2.3.jar
2 jackson-core-2.2.3.jar
file:///D:/Downloads/Jackson JSON-Pojo Parsing Error.html 10/11
13/10/21 23:27 Jackson JSON-Pojo Parsing Error.html
3 jackson-annotations-2.2.3.jar
Once you compiled the class successfully you can run them as you run any other Java program in Eclipse, as shown here or you can run the JAR file
using the command line as shown
here.
In short, The “com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: Unrecognized field XXX, not marked as ignorable” error
comes when you try to parse JSON to a Java object which doesn’t contain all the fields defined in JSON. You can solve this error by either disabling
the feature of Jackson which tells it to fail if encounter unknown properties or by using annotation @JsonIgnoreProperties at the class level.