pratiksha
pratiksha

Reputation: 15

JSON to CSV conversion using Java

Is there any way to convert multiple JSON files into one CSV-file ?

My JSON file is like this:

{
  "Title" : {
      "name" : "ABC",
      "id" : "1",
      "job": "Teacher"    
      },
      "Circle":{
                "area":"2R"
        },
        "Triangle":{
                "length":"45"
        }
}

If you will see, this JSON has 3 roots and different elements under each root. How to convert this JSON to CSV so that it can be opened in excel and can be viewed as follows:

Title
Name  ABC
id    1
job   Teacher
Circle
area  2r
Triangle
length  45

Can someone please suggest?

Upvotes: 0

Views: 3158

Answers (2)

skap
skap

Reputation: 513

There is a library json2flat. It converted your json

{
  "Title" : {
      "name" : "ABC",
      "id" : "1",
      "job": "Teacher"    
      },
      "Circle":{
                "area":"2R"
        },
        "Triangle":{
                "length":"45"
        }
}

to the following

/Title/name,/Title/id,/Title/job,/Circle/area,/Triangle/length
"ABC","1","Teacher","2R","45" 

Hope it helps. After all it depends upon users how they want to interpret it.

Upvotes: 1

facundofarias
facundofarias

Reputation: 3043

It would say that you should first parse you JSON with something like Jackson, and then you can write down a CSV file by using their extension. That's an option.

Other way might be to use an external tool to do the conversion, such as json2csv.

Hope it helps!

Upvotes: 1

Related Questions