How to parse a double quote at start of field in csv using jackson-dataformat-csv CsvMapper

24 Views Asked by At

I would like to parse the following csv file :

Name;Quantity
"foo;bar";1
"foo"bar;2

The expected result is :

[[foo;bar , 1] , ["foo"bar , 2]]

Currently, my code is the following :

CsvMapper mapper = new CsvMapper();
CsvSchema schema = mapper.schemaFor(List.class).withColumnSeparator(';');
MappingIterator<List<String>> it = mapper.readerForListOf(String.class)
            .with(schema)
            .with(CsvParser.Feature.WRAP_AS_ARRAY)
            .readValues(textInput);

The issue is that jackson throws an error due to the first field of the second line starting with a quote, but not ending with one (com.fasterxml.jackson.databind.JsonMappingException: Unexpected character ('b') Expected column separator character (';' (code 59)) or end-of-line at [...]).

If the quotes are starting in the middle of the field, then, there is no issue. For instance, fo"ba"r is correctly parsed.

I can solve the issue for the second row by changing the CsvSchema to :

CsvSchema schema = mapper.schemaFor(List.class).withColumnSeparator(separator).withoutQuoteChar();

but in that case, the first row is not correctly parsed since it is detected as having 3 columns.

Is there a way to parse csv files containing fields starting with quotes and with fields surrounded by quotes ?

0

There are 0 best solutions below