yaylitzis
yaylitzis

Reputation: 5544

Convert JSON to Object throws JsonMappingException "Can not deserialize instance of class out of START_ARRAY token"

I am trying to get information about my Trello boards using their REST API (link) and save it in a Java Object. I followed the initial steps and running this command I get all the info I want:

curl https://api.trello.com/1/members/me/boards?fields=name,url&key={apiKey}&token={apiToken}

which returns data in the following format:

[
  {
   "name": "Greatest Product Roadmap",
   "id": "5b6893f01cb3228998cf629e",
   "url": "https://trello.com/b/Fqd6NosI/greatest-product-roadmap"
  },
  {
    "name": "Never ending Backlog",
    "id": "5b689b3228998cf3f01c629e",
    "url": "https://trello.com/b/pLu77kV7/neverending-backlog"
  },
  //....
{

I create a Board Class

@JsonIgnoreProperties(ignoreUnknown = true)
public class Board {
    private String name;
    private String shortLink;
    private String idBoardSource;
    private String id;
    private String url;

    public Board() {
    }

    //getters, setters
}

In my servlet, I execute:

//Get the response
String command = "curl -X GET https://api.trello.com/1/members/me/boards?fields=name,url&key={apiKey}&token={apiToken}";
ProcessBuilder processBuilder = new ProcessBuilder(command.split(" "));
processBuilder.directory();
Process process = processBuilder.start();
InputStream inputStream = process.getInputStream();

//Convert the InputStream into String
String jsonString = IOUtils.toString(inputStream, StandardCharsets.UTF_8);

//try to create an Board class
Board board = new ObjectMapper().readValue(jsonString, Board.class);

However, I get an error:

org.codehaus.jackson.map.JsonMappingException: Can not deserialize instance of gr.kotronis.trello.Board out of START_ARRAY token

I tried and added @JsonIgnoreProperties(ignoreUnknown = true) but the error still comes up.. I searched for similar questions about this error but I didn't find a solution.. The format of the response data is correct, so I don't understand why I am getting this error...

Upvotes: 0

Views: 3009

Answers (1)

Alexander Terekhov
Alexander Terekhov

Reputation: 894

The problem is that you need to deserialize JSON array as java array, or java List. Here you can find the answer How to use Jackson to deserialise an array of objects.

Upvotes: 1

Related Questions