Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting JSON to CSV

Tags:

java

json

rest

csv

I have a JSON of the format below and I am trying to write to CSV:

{
    "results": [{

            "geo_position": {
                "Field1": 11,
                "Filed2": 12
            },
            "Field3": 13,
            "Filed4": 14,
            "Field5": 15
        },

        {
            "geo_position": {
                "Field1": 21,
                "Filed2": 22
            },
            "Field3": 23,
            "Filed4": 24,
            "Filed5": 25
        }
    ]
}

I am expecting output like:

field1,field2,field3,field4,field5
11,12,13,14,15
21,22,23,24,25

I am getting output CSV as below:

    geo_position,field3,field4,field5
   {Field1:11,Field2:12}, 13,14,15
   {Field2:21,Field2:22},23,24,25

My java code:

JSONObject jsonObj = new JSONObject(jsonArray);
System.out.println(jsonObj);
JSONArray docs = jsonObj.getJSONArray("results");
File file=new File("C:/fromJSON2.csv");
String csv = CDL.toString(docs);
FileUtils.writeStringToFile(file, csv);

Can some one help me to figure out why I am getting in different format. What should I do to get in the format I expect?

like image 933
chiru Avatar asked Dec 04 '13 23:12

chiru


People also ask

Can I convert JSON to CSV?

After you have the JSON data open in Excel you can save the file as a CSV.

Can we convert JSON to CSV in Excel?

This is using the open source JSON library, so just download JSON. js into the same folder you saved the code below into, and it will parse the static JSON value in json3 into CSV and prompt you to download/open in Excel.

What is the CSV file format for a JSON file?

This is the CSV file ‘csv_format.csv’ created from the JSON file ‘json_string.json’. Note that you should close the CSV file to save the contents of the CSV file (csv_file.close ()).

How do I convert JSON to CSV in Python?

Convert JSON to CSV. Step 1: Select your input. Enter Data. Choose File. Enter URL. Examples: Step 2: Choose output options (optional) Step 3: Generate output.

How do I import a JSON file into Excel?

1 Upload your JSON file by clicking the green button (or paste your JSON text / URL into the textbox) 2 (Press the cog button on the right for advanced settings) 3 Download the resulting CSV file when prompted 4 Open your CSV file in Excel (or Open Office)

How do I convert a JSON file to text?

Upload your JSON file by clicking the blue button (or paste your JSON text / URL into the textbox) (Press the cog button on the right for advanced settings) Convert up to 1 MB for free every 24 hours.


1 Answers

There are some typos in the data.

You can try json2flat for converting JSON docs to get an equivalent CSV representation.
If you want to try for more JSON doc click here.

For the JSON data :

{
    "results": [{

            "geo_position": {
                "Field1": 11,
                "Field2": 12
            },
            "Field3": 13,
            "Field4": 14,
            "Field5": 15
        },

        {
            "geo_position": {
                "Field1": 21,
                "Field2": 22
            },
            "Field3": 23,
            "Field4": 24,
            "Field5": 25
        }
    ]
}

Equivalent CSV representation :

results/Field3,results/Field4,results/Field5,results/geo_position/Field1,results/geo_position/Field2
13.0,14.0,15.0,11.0,12.0
23.0,24.0,25.0,21.0,22.0

The code is also preety simple.

JFlat flatMe = new JFlat(jsonString);
flatMe
    .json2Sheet()
    .headerSeparator("/")
    .write2csv("test.csv");

This will write the result to test.csv file.

like image 188
skap Avatar answered Sep 22 '22 13:09

skap