1

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?

Mike Dias
  • 195
  • 1
  • 11
  • That CSV file is corrupt, since it didn't quote / escape the value containing a separator character. Don't try to parse it. Fix the code creating the file instead. – Andreas Nov 16 '18 at 01:35
  • Unfortunately, it's not possible to change the file write. I have to read them in this exact format. – Mike Dias Nov 16 '18 at 04:42
  • Then fix the line, by changing `|` to ``\|`` if the number of preceding ``\"`` is odd. – Andreas Nov 16 '18 at 16:46
  • *FYI:* If the line contains ``\"``, then you need to write ``String csvLine = "2.0.0|2018-11-04 10:10:14,471|eventname|{\\"fieldA\\":\\"value with separator | inside\\",\\"fieldB\\":\\"54321\\"}|123456|";`` – Andreas Nov 16 '18 at 16:48
  • Since the format is always the same you can exploit the spaces between the separator in the JSON: replace `" | "` (space | space) with a string, say `_NONSEP_`, then split the CSV, finally replace `_NONSEP_` in the JSON with the `" | "` again. This is dirty but so is your format. At least this way is easy. Hope this helps. – bitsmanent Jun 27 '19 at 08:53

0 Answers0