Hi Can someone please guide me

0 votes

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.

        

Mar 5, 2021 in Java by Priya

edited Mar 4, 2025 316 views

No answer to this question. Be the first to respond.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP