Skip to content
Advertisement

deserialization of a json array to a list and retaining the array order from json within list

I have the following json that is mapped to a POJO using Jackson mapper. How do I retain the order of array columns from json during deserialization ? What annotation should I use ?

JSON:

{
"columnNames": [
   "FirstName",
   "LastName",
   "UserName"
],
"values": [
    [
        "John",
        "Smith",
        "jsmith"
    ],
    [
        "Tim",
        "Cook",
        "tcook"
    ]
]

}

POJO:

public class Data implements Serializable {

private List<String> columnNames = new ArrayList<String>();

private List<ArrayList<String>> values = new ArrayList<ArrayList<String>>();

public List<String> getColumnNames() {
    return columnNames;
}

public void setColumnNames(List<String> columnNames) {
    this.columnNames = columnNames;
}

public List<ArrayList<String>> getValues() {
    return values;
}

public void setValues(List<ArrayList<String>> values) {
    this.values = values;
}

}

  • Expected getColumnNames(): {“FirstName”,”LastName”,”UserName”}
  • Actual getColumnNames(): {“UserName”,”FirstName”,”LastName”}

I am new to Jackson mapping so any help is appreciated.

Advertisement

Answer

Jackson mapper fill the ArrayList maintaining the order of JSON. If you want a different order you can use the annotation @JsonPropertyOrder.

(https://fasterxml.github.io/jackson-annotations/javadoc/2.8/com/fasterxml/jackson/annotation/JsonPropertyOrder.html)

Advertisement