I have been searching how to convert my json array to CSV file but could not get clear answer. If anyone have done it could you please help me how to convert it or just share the like of any useful documentation, Thank you.
3
-
1not using Android Studio, I mean using Java, I used this before tutorialspoint.com/how-to-convert-a-json-array-to-csv-in-javaAzhagthott– Azhagthott2020-02-25 07:15:27 +00:00Commented Feb 25, 2020 at 7:15
-
1@FranciscoBarrios Thank you for you quick response. I tried to use that code but CDL class is not working. do we need to add any dependency? if so could you please share?Imperial Coop– Imperial Coop2020-02-25 07:24:18 +00:00Commented Feb 25, 2020 at 7:24
-
1ahh yes, actually this is the class you can see here jar-download.com/artifacts/org.json/json/20170516/source-code/…Azhagthott– Azhagthott2020-02-25 07:55:40 +00:00Commented Feb 25, 2020 at 7:55
Add a comment
|
2 Answers
Library Link : https://github.com/opendevl/Json2Flat
Sample Output Like This : https://j2flateval.herokuapp.com
// 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
}
]
}
// 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.
// 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
Comments
You can use Jackson.
The dependencies:
<dependency>
<groupId>com.fasterxml.jackson.dataformat</groupId>
<artifactId>jackson-dataformat-csv</artifactId>
<version>2.9.8</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
</dependency>
A Code example:
@JsonPropertyOrder({"firstName", "lastName", "age"})
public class Person {
private String firstName;
private String lastName;
private Integer age;
public Person()
{}
public Person(String firstName, String lastName, Integer age) {
this.firstName = firstName;
this.lastName = lastName;
this.age = age;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
}
Now the consumer:
public class Consumer {
private ObjectMapper mapper;
private CsvMapper csvMapper;
public static void main(String[] args) {
csvMapper = new CsvMapper();
mapper = new ObjectMapper();
List<Person> list = new ArrayList<>();
simpleList.add(new Person("John", "Wolf", 26));
Consumer c = new Consumer();
String json = c.createJson(list);
System.out.println("The Json file:" + json);
String csvStr = c.createCsv(list);
System.out.println("The Csv file:" + csvStr);
}
private String createJson(List<Person> list) throws JsonProcessingException {
return mapper.writerWithDefaultPrettyPrinter().writeValueAsString(list);
}
private String createCsvList<Person> list) throws JsonProcessingException {
CsvSchema schema = csvMapper.schemaFor(Person.class).withHeader();
return csvMapper.writer(schema).writeValueAsString(list);
}
}
Json output:
[ {
"firstName" : "John",
"lastName" : "Wolf",
"age" : 26
} ]
Csv output:
firstName,lastName,age
John,Wolf,26