0

I am novice in java and I want to convert csv file into json

I'am using the following code example to convert csv file into json file, the things is, I have got an error on the" withSchema(bootstrap) " it says : "The method with(CsvSchema) is undefined for the type ObjectReader" and I do not know how to fix it, I have change maven dependencies to the version 2.9.0, try withtype(bootstrap) withCsvSchema(bootstap) but still not working .

Thanks

Here is the code :

package CsvData;

import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.Map;

import com.fasterxml.jackson.databind.MappingIterator;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.dataformat.csv.CsvMapper;
import com.fasterxml.jackson.dataformat.csv.CsvSchema;

public class csvjson {

    public static void main(String[] args) throws Exception {
        File input = new File("/x/data.csv");
        File output = new File("/x/data.json");

        List<Map<?, ?>> data = readObjectsFromCsv(input);
        writeAsJson(data, output);
    }

    public static List<Map<?, ?>> readObjectsFromCsv(File file) throws IOException {
        CsvSchema bootstrap = CsvSchema.emptySchema().withHeader();
        CsvMapper csvMapper = new CsvMapper();
        MappingIterator<Map<?, ?>> mappingIterator = csvMapper.reader(Map.class).with(bootstrap).readValues(file);

        return mappingIterator.readAll();
    }

    public static void writeAsJson(List<Map<?, ?>> data, File file) throws IOException {
        ObjectMapper mapper = new ObjectMapper();
        mapper.writeValue(file, data);
    }
}
6
  • Add the error message please Commented Apr 26, 2017 at 12:03
  • What's the contents of the returned List<Map>? Commented Apr 26, 2017 at 12:20
  • Did you try to find the JavaDoc for that ObjectReader class ? Commented Apr 26, 2017 at 12:25
  • @AlexT. it contains my csv data Commented Apr 26, 2017 at 12:38
  • @Jens the error is : Exception in thread "main" java.lang.Error: Unresolved compilation problem: The method with(CsvSchema) is undefined for the type ObjectReader Commented Apr 26, 2017 at 13:04

2 Answers 2

1

I tried with version 2.2.1 and it works fine for me.

<dependency>
    <groupId>com.fasterxml.jackson.dataformat</groupId>
    <artifactId>jackson-dataformat-csv</artifactId>
<version>2.2.1</version>

Here is the Java method

public static List<Map<?, ?>> readObjectsFromCsv(File file) throws IOException {
    CsvSchema bootstrap = CsvSchema.emptySchema().withHeader();
    CsvMapper csvMapper = new CsvMapper();
    MappingIterator<Map<?, ?>> mappingIterator = csvMapper.reader(Map.class).with(bootstrap).readValues(file);

    return  mappingIterator.readAll();
}

Even document says it works since 2.2 but dnt know why it is not working for 2.9.0.

Sign up to request clarification or add additional context in comments.

Comments

0

Correct method name is withSchema

csvMapper.reader(Map.class).withSchema(bootstrap).readValues(file);

Comments

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.