Question

I am attempting to parse a csv file with opencsv. The last column in the file has a parameter which is actually json. This json is enclosed in "". The problem is opencsv is removing some " from inside the json causing my code to break.

    CSVReader reader = new CSVReader(new FileReader("c:\\Json.csv"), ',');
nextLine = reader.readNext();
        nextLine[6];

Has anyone seen this before?

example of json as found in the csv

"{"type":"Polygon","coordinates":[[[-66.9,18.05],[-66.9,18.05]]]}"
Was it helpful?

Solution

Technically, your JSON is not valid CSV format unless you escape the quotation marks you want to keep. Double quote characters can be escaped with an additional double quote character (as mentioned in the RFC) or with a backslash (as expected by the default parameters in CSVReader).

So in your example, the CSV content should be:

"{""type"":""Polygon"",""coordinates"":[[[-66.9,18.05],[-66.9,18.05]]]}"

or

"{\"type\":\"Polygon\",\"coordinates\":[[[-66.9,18.05],[-66.9,18.05]]]}"

If you want it to be read/parsed with all the internal quotation marks (but without the surrounding quotation marks). CSVReader will read both as:

{"type":"Polygon","coordinates":[[[-66.9,18.05],[-66.9,18.05]]]}

Also note that you can tell CSVReader to use different characters for quotes and escaping, but you should probably stick with the default since they are more universally accepted.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top