Search code examples
javajsoncsvorg.json

CSV to JSON in JAVA (org.json) Headers to keys not working


This is the content of my CSV File:

FID,OBJECTID,SHAPE,LAGE,GRILLPLATZ_ID,RESERVIERUNG,WEBLINK1,SE_ANNO_CAD_DAT
"GRILLPLATZOGD.6748,6748,POINT (16.465255884594104 48.19018769574157),""22., Donauinsel, ca. 350 Meter stromab der Steinspornbrücke (Inselmitte, Erdwall)"",15,ja,http://www.wien.gv.at/amtshelfer/umwelt/wasserbau/donauinsel/grillplatzreservierung.html,"
"GRILLPLATZOGD.6749,6749,POINT (16.48177464603615 48.183356069714286),""22., Neue Donau, linkes Ufer, zwischen Steinspornbrücke und Waluliso Brücke (bei km 5,1) (Dammbereich) "",16,ja,http://www.wien.gv.at/amtshelfer/umwelt/wasserbau/donauinsel/grillplatzreservierung.html,"
"GRILLPLATZOGD.6750,6750,POINT (16.460158556964053 48.177745677669925),""11., Donaukanal, Alberner Hafenzufahrtsstraße, Nähe Margetinstraße"",0,nein,http://www.wien.gv.at/umwelt/wald/freizeit/grillen/,"
"GRILLPLATZOGD.6751,6751,POINT (16.22577870779843 48.20612009507929),""14., Auhof - Retentionsbecken"",0,nein,http://www.wien.gv.at/umwelt/wald/freizeit/grillen/,"

My program reads the file and then uses the org.json lib to produce a JSONArray filled with JSONObjects from the CSV.

The output of my program looks like this atm:

[{"FID":"GRILLPLATZOGD.6748,6748,POINT (16.465255884594104 48.19018769574157),\"22., Donauinsel, ca. 350 Meter stromab der Steinspornbrücke (Inselmitte, Erdwall)\",15,ja,http://www.wien.gv.at/amtshelfer/umwelt/wasserbau/donauinsel/grillplatzreservierung.html,"} and so on...

My problem is, that only the first part of the header (FID) from the CSV is used to build a JSON Key, the rest of the header (OBJECTID,SHAPE,LAGE,GRILLPLATZ_ID,RESERVIERUNG,WEBLINK1,SE_ANNO_CAD_DAT) is ignored.

Here is a snippet of my code from the CSV to JSON part.

import org.json.CDL;
import org.json.JSONArray;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

public class CSVReader {

    public static void main(String[] args) {
        String path = ""; //Insert path to CSV-File here
        StringBuilder content = new StringBuilder();
        String line;
        String stringtoJSON;

        try {
            BufferedReader reader = new BufferedReader(new FileReader(path));
           
            while ((line = reader.readLine()) != null) {
                content.append(line);
                content.append(System.lineSeparator());
            }
            stringtoJSON = content.toString();
            System.out.println(stringtoJSON);
            
            JSONArray jsonArray = CDL.toJSONArray(stringtoJSON);
            System.out.println(jsonArray);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
   

One JSON Object of the Array should look like this:

{"FID": "GRILLPLATZOGD.6748", "OBJECTID": "6748", "SHAPE": "POINT (16.465255884594104 48.19018769574157)", "LAGE": "22., Donauinsel, ca. 350 Meter stromab der Steinspornbrücke (Inselmitte, Erdwall)","GRILLPLATZ_ID": "15","RESERVIERUNG": "ja","WEBLINK1":"http://www.wien.gv.at/amtshelfer/umwelt/wasserbau/donauinsel/grillplatzreservierung.html"}

Found the solution with the help of @Skilled_Teaser

I altered his method to this:

public static String removeUnnecessaryQuotes(String s){
         String withoutQuotes;
         withoutQuotes = s.substring(0).replaceAll("\"", "");
         withoutQuotes.substring(0).replaceAll("\"\"", "\"");
         return withoutQuotes;
    }

and now it works like a charm. Thanks for the help.


Solution

  • Probably there's a problem with your CSV file. Try the one below.

    FID,OBJECTID,SHAPE,LAGE,GRILLPLATZ_ID,RESERVIERUNG,WEBLINK1,SE_ANNO_CAD_DAT
    "GRILLPLATZOGD.6748",6748,POINT (16.465255884594104 48.19018769574157),"22., Donauinsel, ca. 350 Meter stromab der Steinspornbrücke (Inselmitte, Erdwall)",15,ja,http://www.wien.gv.at/amtshelfer/umwelt/wasserbau/donauinsel/grillplatzreservierung.html
    "GRILLPLATZOGD.6749",6749,POINT (16.48177464603615 48.183356069714286),"22., Neue Donau, linkes Ufer, zwischen Steinspornbrücke und Waluliso Brücke (bei km 5,1) (Dammbereich)",16,ja,http://www.wien.gv.at/amtshelfer/umwelt/wasserbau/donauinsel/grillplatzreservierung.html
    "GRILLPLATZOGD.6750",6750,POINT (16.460158556964053 48.177745677669925),"11., Donaukanal, Alberner Hafenzufahrtsstraße, Nähe Margetinstraße",0,nein,http://www.wien.gv.at/umwelt/wald/freizeit/grillen/
    "GRILLPLATZOGD.6751",6751,POINT (16.22577870779843 48.20612009507929),"14., Auhof - Retentionsbecken",0,nein,http://www.wien.gv.at/umwelt/wald/freizeit/grillen/
    

    Consider to change the structure of your CSV file otherwise you'll need to use regex to split arguments into array and then create JSON.

    Edit:

    Okay, I found the problem. Please, add the method below and make the following change

    while ((line = reader.readLine()) != null) {
                content.append(line);
                content.append(System.lineSeparator());
            }
    

    Replace to:

    while ((line = reader.readLine()) != null) {
            content.append(removeUnnecessaryQuotes(line));
            content.append(System.lineSeparator());
        }
    

    And add method:

    public static String removeUnnecessaryQuotes(String s){
        return s.substring(0, s.length() - 1).substring(1).replaceAll("\"\"", "\"");
    }