12

I am using following code:

CsvSchema bootstrap = CsvSchema.emptySchema().withHeader();
ObjectMapper mapper = new CsvMapper();
File csvFile = new File("input.csv"); // or from String, URL etc
Object user = mapper.reader(?).withSchema(bootstrap).readValue(new File("data.csv"));
mapper.writeValue(new File("data.json"), user);

It throws an error in my IDE saying cannot find symbol method withSchema(CsvSchema) but why? I have used the code from some examples.

I don't know what to write into mapper.reader() as I want to convert any CSV file.
How can I convert any CSV file to JSON and save it to the disk?

What to do next? The examples

Daniel Ruf
  • 7,359
  • 11
  • 61
  • 112

1 Answers1

29

I think, you should use MappingIterator to solve your problem. See below example:

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 JacksonProgram {

    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);
    }
}

See this page: jackson-dataformat-csv for more information and examples.

Michał Ziober
  • 34,365
  • 17
  • 89
  • 132
  • but where does this User.class come from? I am using the scheme from the first line of each CSV file so the User.class does not exist and is also not needed? – Daniel Ruf Nov 04 '13 at 11:18
  • and then converting to JSON? This is just the part to read/parse CSV – Daniel Ruf Nov 04 '13 at 11:19
  • I thought you have User class or something like that. Let me think about that. – Michał Ziober Nov 04 '13 at 11:23
  • No, I want to convert it directly to a JSON file and save it on the disk. – Daniel Ruf Nov 04 '13 at 11:26
  • 2
    Yes, you can do this with `mapper.configure(SerializationFeature.INDENT_OUTPUT, true);` or with `mapper.writerWithDefaultPrettyPrinter().writeValue(file, data);` – Michał Ziober Nov 04 '13 at 12:42
  • 1
    Hi, can we inverse the process , from jsonObject to csv , using the same library ? – Ussopokingo Dec 03 '19 at 10:54
  • @Ussopokingo, yes, we can do that using [jackson-dataformats-text](https://github.com/FasterXML/jackson-dataformats-text/tree/master/csv). Take a look at [Parsing JSON with a random field (java)](https://stackoverflow.com/questions/55577959/parsing-json-with-a-random-field-java) – Michał Ziober Dec 03 '19 at 10:57