schrom
schrom

Reputation: 1651

Parse CSV using Jackson with header into generic list gives exception

I'm using Jackson and Kotlin to read a simple CSV. This code is working fine

fun main(args: Array<String>) {
   val csv = "name,title,size\nabc,def,3\n"
   val data : List<List<String>> = CsvMapper()
      .readerForListOf(java.lang.String::class.java)
      //.with(CsvSchema.emptySchema().withHeader())
      .with(CsvParser.Feature.WRAP_AS_ARRAY)
      .readValues<List<String>>(csv)
      .readAll()

   println(data)
}

and is returning "[[name, title, size], [abc, def, 3]]" as expected. But as you can see, the CSV contains a header I'd like to ignore/skip and not be part of the result data.

When I comment in the CsvSchema line to skip the header, my code is failing with

Exception in thread "main" com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot deserialize value of type `java.util.ArrayList<java.lang.String>` from Object value (token `JsonToken.START_OBJECT`)

Every tutorial states this would be the proper way to handle headers, so I have no clue what's going on.

Upvotes: 0

Views: 49

Answers (0)

Related Questions