I have a task to convert csv file to JSON. Below is the code:
import java.io.File;
import java.util.List;
import java.util.Map;
import com.fasterxml.jackson.databind.MappingIterator;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.dataformat.csv.CsvMapper;
import com.fasterxml.jackson.dataformat.csv.CsvSchema;
public class ConvertCSVtoJson {
@SuppressWarnings("unchecked")
public static void main(String[] args) throws Exception {
File input = new File("C:\\Users\\prshanka\\Documents\\Report.csv");
CsvSchema csvSchema = CsvSchema.builder().setUseHeader(true).build();
CsvMapper csvMapper = new CsvMapper();
List<Object> readAll = (csvMapper).readerFor(Map.class).with(csvSchema).readValues(input).readAll();
ObjectMapper mapper = new ObjectMapper();
mapper.enable(SerializationFeature.INDENT_OUTPUT);
for (Object row : readAll) {
Map<String, String> map = (Map<String, String>) row;
String fileName = map.get("fileName");
File output = new File("C://Users//prshanka//Documents//Target"+fileName+".txt");
mapper.writerWithDefaultPrettyPrinter().writeValue(output, row);
}
}
}
I am getting error in this line-
List<Object> readAll = (csvMapper).readerFor(Map.class).with(csvSchema).readValues(input).readAll();
Please guide me, getting error- "The method readerFor(Class<Map>) is undefined for the type CsvMapper". I have added all the dependencies on build path.