I am trying to develop an application that converts JSON to CSV .
I use JACKSON to parse JSON and write CSV .
Here is an example JSON I am trying to parse:
{ "_id" : 0, "name" : "aimee Zank", "scores" : [ { "type" : "exam", "score" : 1.463179736705023 }, { "type" : "quiz", "score" : 11.78273309957772 }, { "type" : "homework", "score" : 6.676176060654615 }, { "type" : "homework", "score" : 35.8740349954354 } ] } { "_id" : 1, "name" : "Aurelia Menendez", "scores" : [ { "type" : "exam", "score" : 60.06045071030959 }, { "type" : "quiz", "score" : 52.79790691903873 }, { "type" : "homework", "score" : 71.76133439165544 }, { "type" : "homework", "score" : 34.85718117893772 } ] } { "_id" : 2, "name" : "Corliss Zuk", "scores" : [ { "type" : "exam", "score" : 67.03077096065002 }, { "type" : "quiz", "score" : 6.301851677835235 }, { "type" : "homework", "score" : 20.18160621941858 }, { "type" : "homework", "score" : 66.28344683278382 } ] }
Two Java classes:
Jsonnode class
public class JsonNode { private String _id; private String name; private Collection<ScoreType> scores; public String get_id() { return _id; } public void set_id(String _id) { this._id = _id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Collection<ScoreType> getScores() { return scores; } public void setScores(Collection<ScoreType> scores) { this.scores = scores; } }
Class ScoreType
public class ScoreType { private String type; private String score; public String getType() { return type; } public void setType(String type) { this.type = type; } public String getScore() { return score; } public void setScore(String score) { this.score = score; } }
Method that converts JSON to CSV
ObjectMapper mapper=new ObjectMapper(); JsonNode jsonNode=mapper.readValue(new File("C:\\...\\...\\...\\test.json"), JsonNode.class); CsvMapper csvMapper=new CsvMapper(); CsvSchema schema=csvMapper.schemaFor(JsonNode.class); schema=schema.withColumnSeparator(';'); ObjectWriter myObjectWriter=csvMapper.writer(schema); FileOutputStream tempFileOutputStream=new FileOutputStream(out+"\\jsontocsv.csv"); BufferedOutputStream bufferedOutputStream=new BufferedOutputStream(tempFileOutputStream); OutputStreamWriter writerOutputStream=new OutputStreamWriter(bufferedOutputStream,"UTF-8"); myObjectWriter.writeValue(writerOutputStream,jsonNode);
Console output:
com.fasterxml.jackson.core.JsonGenerationException: CSV generator does not support
Array Values ββfor Properties
CSV Output:
"0";"aimee Zank";
This is what I have done so far.
So, I ran into two problems:
1) The output CSV is not completed, it creates only one line and does not record points.
2) Error in the console.
I am using the following JACKSON :
<dependency> <groupId>com.fasterxml.jackson.dataformat</groupId> <artifactId>jackson-dataformat-csv</artifactId> <version>2.4.0</version> </dependency>
Can someone help me solve these problems?
I hope I was clear enough.
CSV Editing I expect:
_id;name;scores.type;scores.score;scores.type;scores.score;scores.type;scores.score 0;aimee Zank;exam;1.46;quiz;11.78;homework;6.67;homework;35.87
Ismail