0% found this document useful (0 votes)
33 views11 pages

Jackson Json Pojo Parsing Error - HTML

Uploaded by

Goxzalo hds
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
33 views11 pages

Jackson Json Pojo Parsing Error - HTML

Uploaded by

Goxzalo hds
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

13/10/21 23:27 Jackson JSON-Pojo Parsing Error.

html

Jackson JSON Parsing Error – UnrecognizedPropertyException: Unrecognized field, not


marked as ignorable [Solved]
Posted by: Javin Paul in Enterprise Java October 16th, 2017 1 Comment 42186 Views

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:

1 Exception in thread "main" com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: <b>Unrecogn

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.

Our POJO class looks like below:

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 }

and the JSON String:

01 {
02

file:///D:/Downloads/Jackson JSON-Pojo Parsing Error.html 1/11


13/10/21 23:27 Jackson JSON-Pojo Parsing Error.html

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.

How to solve this problem


Here are steps to solve this problem and get rid of this errorr:

1) Configure Jackson’s ObjectMapper to not fail when encounger unknown properties

You can do this by disabling DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES property of ObjectMapper as shown below:

1 // Jackson code to convert JSON String to Java object


2
3 ObjectMapper objectMapper = new ObjectMapper();
4
5 objectMapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
6
7 Person p = objectMapper.readValue(JSON, Person.class);
8
9 System.out.println(p);

Now, the error will go away but the Output is not what you expected, it will print following:

1 Person [id=0, name=null, city=null, phone=0]

file:///D:/Downloads/Jackson JSON-Pojo Parsing Error.html 2/11


13/10/21 23:27 Jackson JSON-Pojo Parsing Error.html

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:

01 static class Community {


02 private List<Person> person;
03
04 public List<Person> getPerson() {
05 return person;
06 }
07
08 public void setPerson(List<Person> person) {
09 this.person = person;
10 }
11
12 }

Now, we will convert the JSON String to this Community class and print each person from the list as shown below:

01 ObjectMapper objectMapper = new ObjectMapper();


02
03 //objectMapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
04
05 Community c = objectMapper.readValue(JSON, Community.class);
06
07 for (Person p : c.getPerson()) {
08
09 System.out.println(p);
10
11 }

This will print the details of a person properly as shown below:

1 Person [id=11, name=John, city=NewYork, phone=7647388372]

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.

Suppose, our JSON String to parse is following:

file:///D:/Downloads/Jackson JSON-Pojo Parsing Error.html 3/11


13/10/21 23:27 Jackson JSON-Pojo Parsing Error.html

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:

1 ObjectMapper objectMapper = new ObjectMapper();


2
3 objectMapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
file:///D:/Downloads/Jackson JSON-Pojo Parsing Error.html 4/11
13/10/21 23:27 Jackson JSON-Pojo Parsing Error.html

4
5 Community c = objectMapper.readValue(JSON, Community.class);

And this will print the person class properly as shown below:

1 Person [id=11, name=John, city=NewYork, phone=7647388372]

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)

So, our Person class now looks like:

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

001 import java.io.IOException;


002 import java.util.List;
003
004 import com.fasterxml.jackson.core.JsonParseException;
005 import com.fasterxml.jackson.databind.DeserializationFeature;
006 import com.fasterxml.jackson.databind.JsonMappingException;
007 import com.fasterxml.jackson.databind.ObjectMapper;
file:///D:/Downloads/Jackson JSON-Pojo Parsing Error.html 5/11
13/10/21 23:27 Jackson JSON-Pojo Parsing Error.html

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

094 public String toString() {


095 return "Person [id=" + id + ", name=" + name + ", city=" + city
096 + ", phone=" + phone + "]";
097 }
098
099 }
100 }

When I run first version of this program, I was greeted with the following error:

01 Exception in thread "main" com.fasterxml.jackson.databind.JsonMappingException: No suitable constructor


02
03 at [Source: java.io.StringReader@5e329ba8; line: 2, column: 3]
04
05 at com.fasterxml.jackson.databind.JsonMappingException.from(JsonMappingException.java:164)
06
07 at com.fasterxml.jackson.databind.deser.BeanDeserializerBase.deserializeFromObjectUsingNonDefault(BeanD
08
09 at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserializeFromObject(BeanDeserializer.java:27
10
11 at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:121)
12
13 at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:2888)
14
15 at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:2034)
16
17 at Hello.main(Hello.java:40)

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.

Now, let’s see the real error:

01 Exception in thread "main" com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: <b>Unrecog


02
03 at [Source: java.io.StringReader@4fbc9499; line: 2, column: 14] (through reference chain: Person["perso
04
05 at com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException.from(UnrecognizedPropertyException.
file:///D:/Downloads/Jackson JSON-Pojo Parsing Error.html 8/11
13/10/21 23:27 Jackson JSON-Pojo Parsing Error.html

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:

1 Person [id=11, name=John, city=NewYork, phone=7647388372]

This means we are able to parse JSON containing unknown attributes successfully in Jackson.

How to compile and run this program?


You can simply copy paste the code into your favorite IDE e.g. Eclipse to compile and run the program.

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.

file:///D:/Downloads/Jackson JSON-Pojo Parsing Error.html 9/11


13/10/21 23:27 Jackson JSON-Pojo Parsing Error.html

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.

For Maven Users

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.

Manually Downloading JAR

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.

file:///D:/Downloads/Jackson JSON-Pojo Parsing Error.html 11/11

You might also like