I have a sample CSV message:
header1,header2,header3
value1,value2,{"name":"John","age":30,"car":null}
How to convert it in form of embedded JSON as in:
{
"header1": "value1",
"header2": "value2",
"header3": "{\"name\":\"John\",\"age\":30,\"car\":null}"
}
I am using Jackson schema builder with default column separator:
CsvSchema.builder().disableQuoteChar().setUseHeader(true).build();
CsvMapper.builder().enable(CsvParser.Feature.IGNORE_TRAILING_UNMAPPABLE, CsvParser.Feature.WRAP_AS_ARRAY).build();
Presented
CSVcontent is broken. Values which contains column separator should be wrapped with a quote character. If we can not change the app which generates it we need to amend it before deserialising process. This example is simple so we can just replace{with|{and}with}|and set|as a quote character. But forJSONpayloads with internal objects we need to replace only the first{and the last}brackets. Code could look like below:Above code prints: