0

This is my first post in StackOverflow!

I have been trying to upload a CSV file, parse it, and create a json file from it.

I found this tutorial using Jackson parser: https://kalliphant.com/jackson-convert-csv-json-example/ (I picked that because of the speed of processing), but I found errors when I tried it and I did not find why am I getting those, I am using intellij IDEA actually, and I tried using reload project and download sources from maven, but it did not solve the issue.

I have been looking for this error on the internet but I did not find any relevant.

I put the exact same code from the tutorial and I am getting those errors:

Errors I got:

Errors I got

Thank you!

AleksanderGD
  • 414
  • 5
  • 10

1 Answers1

0

The errors you're getting appear to be related to bad imports at your class file. Now even if that was OK, what you're trying to do you still not work.

Firstly, your CSV file is missing a header (above country), secondly as with normal JSON serialization/deserialization you need to perform this action against an object (a simple pojo). In your case your attempting to do this using object which is wrong -- both syntactically as well as conceptually.

With the above on hand try the following. Modify your CSV file to look like so:

country population  mortality
spain   13000000    10000
france  30000000    15000
united kingdom  40000000    22000
belgium 20000000    50000
us  25000000    30000

The try the following code:

public class CsvParser {

    public static void main(String... args) {

        CsvSchema schema = CsvSchema
            .emptySchema()
            .withHeader();

        ObjectReader reader = new CsvMapper()
            .readerFor(MortalityEntry.class)
            .with(schema);

        List<MortalityEntry> results = null;
        try {
            MappingIterator<MortalityEntry> iterator = reader.readValues(Paths.get("/tmp/input.csv").toFile());
            results = iterator.readAll();
        } catch (IOException e) {
            e.printStackTrace();
        }

        ObjectMapper objectMapper = new ObjectMapper();
        try {
             objectMapper.writeValue(Paths.get("/tmp/output.json").toFile(), results);
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    private static class MortalityEntry  {

        private String country;
        public String getCountry() { return country; }
        public void setCountry(String country) { this.country = country; }

        private Integer population;
        public Integer getPopulation() { return population; }
        public void setPopulation(Integer population) { this.population = population; }

        private Integer mortality;
        public Integer getMortality() { return mortality; }
        public void setMortality(Integer mortality) { this.mortality = mortality; }

    }

}

As you can see I'm using a simple pojo MortalityEntry to deserialize (from CSV) and serialize (to JSON), letting Jackson do its magic.

This simple example should be enough to get you going.

akortex
  • 5,067
  • 2
  • 25
  • 57
  • Hello @akortex91 , thank you for your reply. That is quite good right, but the issue here is that I don't want to limit this parser to only that file, the headers won't be the same for each time, as here we have country, population and mortality, maybe the other csv file will have other information like a table of products, their prices, numbers etc... I hope that you understand what I am trying to realise here. – Aymen H Aug 04 '20 at 20:49
  • I see what you're trying to achieve but they way you want to do won't cut it for you. Jackson expects a concrete class to deserialize to which in case of the CSV parser (when the file has a header) uses it to match the header values to fields. Thus attempting to use something too generic like `Object` won't work. You could potentially alter the code example above to accept a target class as well as use or not a header but you still can't get around having POJO classes. Also if you found the answer OK please make sure to upvote it. – akortex Aug 05 '20 at 08:07
  • Thank you, I upvoted your answer as it works for other situation but not the way I would like it to.A quick update is that I managed to do that but I found out that the file size will be too big unfortunately. I am looking for another way to do it, maybe I will add encryption or something else. Appreciate the help :) – Aymen H Aug 06 '20 at 09:01