Json Mapping Exception can not deserialize instance out of START_ARRAY token

Marcin Erbel picture Marcin Erbel · Nov 26, 2014 · Viewed 62.4k times · Source

I'm trying to parse my json request to my model. I dunno what is wrong in this code. Syntax of json looks correct and annotations on Java model also. I don't know why I'm getting error like:

Caused by: org.codehaus.jackson.map.JsonMappingException: Can not deserialize instance of ParametersType out of START_ARRAY token
(through reference chain: Document["parameters"])

Java model:

@JsonIgnoreProperties( ignoreUnknown = true )
public class Document {

   @XmlElement( required = true )
   @JsonProperty( "templateId" )
   protected String templateId;

   @JsonProperty( "parameters" )
   @XmlElement( required = true )
   protected ParametersType parameters;

   @JsonProperty( "documentFormat" )
   @XmlElement( required = true )
   protected DocumentFormatType documentFormat;

...}

@JsonIgnoreProperties( ignoreUnknown = true )
public class ParametersType {

    @JsonProperty( "parameter" )
    protected List<ParameterType> parameter;

...}

@JsonIgnoreProperties( ignoreUnknown = true )
public class ParameterType {

    @XmlElement( required = true )
    @JsonProperty( "key" )
    protected String key;

    @XmlElement( required = true )
    @JsonProperty( "value" )
    @XmlSchemaType( name = "anySimpleType" )
    protected Object value;

    @JsonProperty( "type" )
    @XmlElement( required = true, defaultValue = "STRING_TYPE" )
    protected ParamType type;

....}

Json code:

{
    "templateId": "123",
    "parameters": [
        {
            "parameter": [
                {
                    "key": "id",
                    "value": "1",
                    "type": "STRING_TYPE"
                },
                {
                    "key": "id2",
                    "value": "12",
                    "type": "STRING_TYPE"
                }
            ]
        }
    ],
    "documentFormat": "PDF"
}

Answer

gregwhitaker picture gregwhitaker · Nov 26, 2014

You have declared parameters as a single object, but you are returning it as an array of multiple objects in your JSON document.

Your model currently defines the parameters node as a ParametersType object:

@JsonProperty( "parameters" )
@XmlElement( required = true )
protected ParametersType parameters;

This means your model object is expecting a JSON document that looks like the following:

{
    "templateId": "123",
    "parameters": {
            "parameter": [
                {
                    "key": "id",
                    "value": "1",
                    "type": "STRING_TYPE"
                },
                {
                    "key": "id2",
                    "value": "12",
                    "type": "STRING_TYPE"
                }
            ]
        },
    "documentFormat": "PDF"
}

But in your JSON document you are returning an array of ParametersType objects. So you need to change your model to be a list of ParametersType objects:

@JsonProperty( "parameters" )
@XmlElement( required = true )
protected List<ParametersType> parameters;

The fact that you are returning an array of ParametersType objects is why the parser is complaining about not being able to deserialize an object out of START_ARRAY. It was looking for a node with a single object, but found an array of objects in your JSON.