How to parse a CSV with JSON fields containing separator?

1.6k Views Asked by At

I have the following CSV file with a JSON field that may contains the separator:

2.0.0|2018-11-04 10:10:14,471|eventname|{"fieldA":"value with separator | inside","fieldB":"54321"}|123456|empty

I tried to use the following code to get the whole field but it is always get split by the inner separator:

String csvLine = "2.0.0|2018-11-04 10:10:14,471|eventname|{\"fieldA\":\"value with separator | inside\",\"fieldB\":\"54321\"}|123456|empty"; 

CsvParserSettings settings = new CsvParserSettings();
settings.getFormat().setDelimiter("|");
settings.getFormat().setQuote('"');
settings.getFormat().setQuoteEscape('\\');

CsvParser parser = new CsvParser(settings);

String[] row = parser.parseLine(csvLine);
System.out.println(row[3]);

Expected output:

{"fieldA":"value with separator | inside","fieldB":"54321"}

Current output:

{"fieldA":"value with separator

How can I tell the CSV parser to not split when the file is between quotes?

0

There are 0 best solutions below