0

Just like in this question, I want to deserialize JSON with a root element using @JsonRootName. But in my case, the value of the root field is an array with exactly one object instead of just an object.

So instead of this

{
  "user":
    {
      "name":"Sam Smith",
      "age":1
  }
}

I have this

{
  "user":[
    {
      "name":"Sam Smith",
      "age":1
  }
]}

I cannot simply use

@JsonRootNode(value="user")
public class User {
    public String name;
    public Integer age;
}

because that would produce a JsonMappingException: Can not deserialize instance of User out of START_ARRAY token.

Is there a way to tell jackson to ignore the array and just look at the object?

Community
  • 1
  • 1
florian
  • 434
  • 2
  • 12
  • I would guess no you cant. Why would you want to? If it's an array just accept it, deserialise the array and get the first (only) item. If there should only ever be one item then what ever produces the JSON should never have serialised an array. – TedTrippin Sep 26 '14 at 11:52
  • I agree, unfortunately I have no control over the serialisation. Of course I can just deserialise the array and get the entry, it’s just less elegant that way – florian Sep 26 '14 at 12:09

0 Answers0