Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

Does anyone have any sample Java code to convert a JSON document to XLS/CSV file? I have tried to search on Google but to no avail.

share|improve this question
See Matt York's answer here: stackoverflow.com/questions/662859/convert-csv-xls-to-json – JMax Aug 24 '11 at 7:59

3 Answers

You could only convert a JSON array into a CSV file.

Lets say, you have a JSON like the following :

{"infile": [{"field1": 11,"field2": 12,"field3": 13},
            {"field1": 21,"field2": 22,"field3": 23},
            {"field1": 31,"field2": 32,"field3": 33}]}

Lets see the code for converting it to csv :

import org.apache.commons.io.FileUtils;

import org.json.CDL;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

public class JSON2CSV {
  public static void main(String myHelpers[]){
     String jsonString = "{\"infile\": [{\"field1\": 11,\"field2\": 12,\"field3\": 13},{\"field1\": 21,\"field2\": 22,\"field3\": 23},{\"field1\": 31,\"field2\": 32,\"field3\": 33}]}"

     JSONObject output = new JSONObject(jsonOut);
     JSONArray docs = response.getJSONArray("infile");

     File file=new File("yourpath/fromJSON.csv");
     String csv = CDL.toString(docs);
     FileUtils.writeStringToFile(file, csv);
  }
}

Now you got the CSV generated from JSON.

It should look like this:

field1,field2,field3
11,22,33
21,22,23
31,32,33

The maven dependency was like,

<dependency>
    <groupId>org.json</groupId>
    <artifactId>json</artifactId>
    <version>20090211</version>
</dependency>
share|improve this answer

you can use commons csv to convert into CSV format. or use POI to convert into xls. if you need helper to convert into xls, you can use jxls, it can convert java bean (or list) into excel with expression language.

Basically, the json doc maybe is a json array, right? so it will be same. the result will be list, and you just write the property that you want to display in excel format that will be read by jxls. See http://jxls.sourceforge.net/reference/collections.html

If the problem is the json can't be read in the jxls excel property, just serialize it into collection of java bean first.

share|improve this answer
How would you map {'foo': {'bar': [1, 2, 3]}} to a spreadsheet? – user647772 Aug 24 '11 at 8:02
That's why I said, if the json object is json single object, not json array, the result will be 1 row in the spreadsheet. If the json object is json array, the rows will be same with the json array size. And 1 again, you have to specify what property you want to write into that spreadsheet. If your example, if you want to display the bar (which the value is array), the column will be display maybe java.lang.Array@xxxxx, but it is an array, right. But if the object is [{name: 'a', 'bar' : [1,2,3]}, {name: 'b', 'bar' : [2,3,4]}], and you want to display the name only, it's feasible, right? – Jef Aug 24 '11 at 8:20

A JSON document basically consists of lists and dictionaries. There is no obvious way to map such a datastructure on a two-dimensional table.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.