Imperial Coop
Imperial Coop

Reputation: 21

How to convert json Array to CSV file using Java

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.

Upvotes: 2

Views: 4288

Answers (2)

raven99
raven99

Reputation: 1421

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

Upvotes: 0

Manikandan Karuppiyah
Manikandan Karuppiyah

Reputation: 138

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

Upvotes: 1

Related Questions