7

This question has been asked many times but I couldn't find the answer that fixes my issue.

I'm trying to convert nested JSON format to CSV format like this :

The JSON structure is arbitrary and could be anything, nested or not.

I'm not suppose to know it, it's a database answer and I need to export this JSON answer into CSV file.

Here is an example

Input :

   {
    "_id": 1,
    "name": "Aurelia Menendez",
    "scores": [
              {
                 "type": "exam",
                 "score": 60.06045071030959
               },
               {
                 "type": "quiz",
                 "score": 52.79790691903873
               },
               {
                "type": "homework",
                "score": 71.76133439165544
               }
             ]
          }

The output I'm looking for :

_id,name,scores.type,scores.score,scores.type,scores.score,scores.type,scores.score  
 1,Aurelia Menendez,exam,60.06...,quiz,52.79...,homework,71.76..

This is an example, it could be any other JSON document.

The idea here is to use dot notation in the CSV column name.

I've already used CDL but the output is not what I want :

_id scores  name
 1  "[{score:60.06045071030959,type:exam},{score:52.79790691903873,type:quiz},{score:71.76133439165544,type:homework}]" Aurelia Menendez

So how can I convert nested JSON to CSV with dot notation and in a generic way ?

Edits

Deserialisation of the JSON with Jackson :

   ObjectMapper mapper=new ObjectMapper();

    JsonNode jsonNode=mapper.readValue(new File("C:\\...\\...\...\\test.json"), JsonNode.class);

Ismail

12
  • 1
    Is there anything you have tried? Commented Jul 16, 2014 at 10:12
  • CDL but I'm not free to set parameters so I can have the output I want. Commented Jul 16, 2014 at 10:16
  • @Tichodroma , Have you ever faced my issue ? Commented Jul 16, 2014 at 12:22
  • Have you considered actually writing some code? (Yes, I know that's a rash thing to suggest.) Commented Jul 16, 2014 at 12:39
  • 1
    @IsmailSen I think that forms of that JSON are limited. Analyze forms of JSON structure you can get from the database, and declare java class or java classes which can reflect this structure. And after that use GSON or another deserializer to process this json structure. Commented Jul 16, 2014 at 13:03

3 Answers 3

3

Like you said :

The JSON structure is arbitrary and could be anything, nested or not.

The JSON to CSV conversion can't be generalized as it varies from user to user and also depends specific requirements.

But still there's a library json2flat which tries to achieve it. But it may differ from user's requirement. Still it's worth a try.

For example for the JSON given above:

{
    "_id": 1,
    "name": "Aurelia Menendez",
    "scores": [
              {
                 "type": "exam",
                 "score": 60.06045071030959
               },
               {
                 "type": "quiz",
                 "score": 52.79790691903873
               },
               {
                "type": "homework",
                "score": 71.76133439165544
               }
             ]
}

can be interpreted as follows :

/_id,/name,/scores/type,/scores/score
1,"Aurelia Menendez","exam",60.06045071030959
1,"Aurelia Menendez","quiz",52.79790691903873
1,"Aurelia Menendez","homework",71.76133439165544
Sign up to request clarification or add additional context in comments.

3 Comments

Suppose that the JSON included another array after "scores" : [ … ], such as perhaps "classes" : [ … ] or "societies" : [ … ]; how would you convert that? Don't get me wrong: what you suggest is sensible (and different from other answers) for the sample data, but the question does state "JSON structure is arbitrary … nested", so ideally your idea should be readily generalizable.
@JonathanLeffler it all depends upon the user. How they want to have an interpretation of the json. Well for the JSON I here goes another sample ::
@JonathanLeffler Here goes the sample as you have asked: { "_id": 1, "name": "Aurelia Menendez", "scores": [ { "type": "exam", "score": 60.06045071030959 } ], "classes" : [ { "sub" : "English" } ], "societies" : [ { "socDemo" : "xyz" } ]}. The output to this JSON will be /_id,/name,/scores/type,/scores/score,/classes/sub,/societies/socDemo 1,"Aurelia Menendez","exam",60.06045071030959,, 1,"Aurelia Menendez",,,"English", 1,"Aurelia Menendez",,,,"xyz".
2

Converting JSON to XLS/CSV in Java has what you are looking for.

Basically, you need to use org.json.CDL to convert from JSON to CSV format

Comments

0

Comments are not convenient place to post longs answers, so I post my answer here.

  1. Analyze your JSON and all possible JSON structures you can get from your database. It should be a limited number of JSON forms.

  2. As you have analyzed your JSON structure build a class/class hierarchy, that fully reflects this structure.

  3. Use JSON serializer/deserializer library at your choice, to deserialize JSON to a java object.

  4. Employ StringBuffer/StringBuilder classes, and iterate over your object information, and build comma delimited (or tab-delimited) strings.

  5. Write strings you have built on the previous stage to the file.

That's it.

6 Comments

Could you please have a look here : stackoverflow.com/questions/24799457/…
@IsmailSen sorry, but I have no experience with Jackson framework.\
In step 4, how can I iterate over the elements of scores collection ?
scores is a collection of objects? implement a loop, that iterates over this collection and builds a string using StringBuilder or StringBuffer class instance.
In the Edits, I deserialize the JSON into a java object jsonNode. When I try to iterate over it, I got Can only iterate over an array or an instance of java.lang.Iterable.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.