Reputation: 765
I am trying to consume a REST Service using RestTemplate. I am having trouble deserializing the JSON response. I am using a custom Deserializer and my JSON has 3 nodes, but it looks like only one is reaching the deserializer. Below are more details.
Below is the response JSON:
{
"Hello": {
"Hi": "Name1",
"Call": "PhoneNumber1"
},
"Hello": {
"Hi": "Name2",
"Call": "PhoneNumber2"
},
"Hello": {
"Hi": "Name3",
"Call": "PhoneNumber3"
}
}
I am using a custom deserializer on the Response Class for attribute Hello using @JsonDeserializer.
When i do a readTree like below:
JsonNode node = jp.getCodec().readTree(jp);
it reaches the deserialize method, it looks like it is having only one node instead of 3 like below. Sometimes it has the first node and sometimes the last. What could be going wrong here?
Thanks in advance for looking at this question and helping out!
ANSWER: As others mentioned, this JSON is invalid and hence Jackson is not able to deserialize it. I had to get the response as a String and then deserialize manually.
Upvotes: 1
Views: 539
Reputation: 20594
JsonNode
is a superclass without specific content behavior. In your example you should get an ObjectNode
but as your properties all have the same name only one "Hello" property will remain. readTree()
is a generic method which do auto cast to your needed return type if possible.
If you really need this you have to move your JSON to an array structure:
// you will get one ArrayNode containing multiple ObjectNode
[
{
"Hello": {
"Hi": "Name1",
"Call": "PhoneNumber1"
}
},
{
"Hello": {
"Hi": "Name2",
"Call": "PhoneNumber2"
}
},
{
"Hello": {
"Hi": "Name3",
"Call": "PhoneNumber3"
}
}
]
or
// you will get one ObjectNode containing one property with an ArrayNode
{
"Hello": [
{
"Hi": "Name1",
"Call": "PhoneNumber1"
},
{
"Hi": "Name2",
"Call": "PhoneNumber2"
},
{
"Hi": "Name3",
"Call": "PhoneNumber3"
}
]
}
Upvotes: 1
Reputation: 10666
That is most likely because that is invalid JSON, you can't have three nodes with the same name.
Or strictly speaking it's not an error but only one of the items named "Hello" will be accounted for as stated in this answer, which is bad since json does not guarantee the order of the items in an object only the order of items in lists.
If you have the possibility to change the response, I would go with creating an array of objects instead.
{
"Hello": [
{
"Hi": "Name1",
"Call": "PhoneNumber1"
},
{
"Hi": "Name2",
"Call": "PhoneNumber2"
},
{
"Hi": "Name3",
"Call": "PhoneNumber3"
}
]
}
Then you would get a JsonNode that is an array of JsonNodes which you can iterate over.
Upvotes: 1