0

I'm writing the below to convert CSV file to JSON but getting java heap memory error.

Can anyone help me to write using the Jackson stream API?

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("C:\\Users\\shivamurthym\\Downloads\\FL_insurance_sample\\FL_insurance_sample.csv");
        File output = new File("C:\\Users\\shivamurthym\\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);
    }
}
HoRn
  • 1,458
  • 5
  • 20
  • 25
Meg
  • 11
  • 1
    Have you tried increasing your maximum heap size? – f1sh Apr 09 '20 at 12:10
  • Can you quantify "large" in terms of "MB" / "GB" ... ? – Fildor Apr 09 '20 at 12:11
  • 1
    In question you could point to a answer [directly convert CSV file to JSON file using the Jackson library](https://stackoverflow.com/questions/19766266/directly-convert-csv-file-to-json-file-using-the-jackson-library/19766556#19766556) from which you got a code. It will be helpful. Instead of `mappingIterator.readAll()` method you can iterate over `CSV` line-by-line using `hasNext` and `next` methods. In that case you do not have to load whole file to memory. – Michał Ziober Apr 09 '20 at 12:18
  • Does this answer your question? [directly convert CSV file to JSON file using the Jackson library](https://stackoverflow.com/questions/19766266/directly-convert-csv-file-to-json-file-using-the-jackson-library) – pczeus Apr 09 '20 at 12:36

0 Answers0