I am getting below error The method readerFor Class Map is undefined for the type CsvMapper

+1 vote
I Have requirement to convert csv to json and generate multiple json files as per records available in csv file.

Please find below code:

import java.io.File;
import java.lang.*;
import java.util.Arrays;
import java.util.List;

import java.util.Map;

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("D:\\Monika\\Test.csv");
        CsvSchema csvSchema = CsvSchema.builder().setUseHeader(true).build();
        CsvMapper csvMapper = new CsvMapper();

        // Read data from CSV file
        List<Object> readAll = Arrays.asList(csvMapper.readerFor(Map.class).with(csvSchema).readValues(input).readAll());
                    ObjectMapper mapper = new ObjectMapper();
        mapper.enable(SerializationFeature.INDENT_OUTPUT);
        // Write JSON formated data to output.json file
           for (Object row : readAll) {
        Map<String, String> map = (Map<String, String>) row;
        String fileName = map.get("fileName");
        File output = new File("D:\\Monika\\"+fileName+".txt");
         mapper.writerWithDefaultPrettyPrinter().writeValue(output, row);
                }
          }
}

i am getting error on line"        List<Object> readAll = Arrays.asList(csvMapper.readerFor(Map.class).with(csvSchema).readValues(input).readAll());".

Kindly suggest.
Dec 29, 2019 in Java by anonymous
• 160 points
3,193 views
Hi,

can anyone help me?

i am trying it from last week.
Hey @Monika, your code looks fine. I guess this might be a dependency issue. Have you added the required dependency files?
Hi,

i added all dependencies also.

PFB details:

pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
                      http://maven.apache.org/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.conversion.ConvertCSVtoJson</groupId>
    <artifactId>ConvertCSVtoJson</artifactId>
    <name>ConvertCSVtoJson</name>
 
    <dependencies>
       <dependency>
         <groupId>com.fasterxml.jackson.core</groupId>
         <artifactId>jackson-databind</artifactId>
         <version>2.2.3</version>
       </dependency>
       <dependency>
         <groupId>com.fasterxml.jackson.core</groupId>
         <artifactId>jackson-core</artifactId>
         <version>2.2.3s</version>
       </dependency>
       <dependency>
         <groupId>com.fasterxml.jackson.dataformat</groupId>
         <artifactId>jackson-dataformat-csv</artifactId>
         <version>2.1.4</version>
       </dependency>      
    </dependencies>
    <version>2.1.4</version>
</project>
Have you added the corresponding jar dependency files to the build path?
yes,i added the corresponding jar files to build path.

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.

Related Questions In Java

+1 vote
0 answers

getting below error when trying to conver csv file to json :The method readerFor(Class<Map>) is undefined for the type CsvMapper

PFB source code: import java.io.File; import java.lang.*; import java.util.Arrays; import java.util.List; import ...READ MORE

Dec 29, 2019 in Java by anonymous
• 160 points

closed Jan 14, 2020 by Kalgi 2,052 views
0 votes
2 answers

What is the use of toString method in Java and how can I use it ?

Whenever you require to explore the constructor ...READ MORE

answered Aug 23, 2018 in Java by Daisy
• 8,140 points
4,109 views
0 votes
2 answers

How can I invoke a method when the method name is in the form of a given string?

You could probably use method invocation from reflection: Class<?> ...READ MORE

answered Aug 19, 2019 in Java by Sirajul
• 59,230 points
3,075 views
0 votes
1 answer

How can I make the return type of a method generic?

First of all, define callFriend: public <T extends ...READ MORE

answered May 19, 2018 in Java by sharth
• 3,370 points
797 views
+17 votes
25 answers

How can I convert String to JSON object in Java?

Hi @Daisy You can use Google gson  for more ...READ MORE

answered Feb 7, 2019 in Java by Suresh
• 720 points
253,658 views
0 votes
2 answers

How to convert a JSON String into Object in Java?

You could probably check out Google's Gson: ...READ MORE

answered Aug 21, 2019 in Java by Sirajul
• 59,230 points
3,266 views
0 votes
1 answer

How to convert or cast hashmap to JSON object in Java, and again convert JSON object to JSON string?

You can use: new JSONObject(map); READ MORE

answered Jun 27, 2018 in Java by Akrati
• 3,190 points
7,005 views
0 votes
1 answer

How can we return a JSON object from a Java Servlet?

response.setContentType("application/json"); // Get the printwriter object from response ...READ MORE

answered Jul 6, 2018 in Java by sharth
• 3,370 points
5,903 views
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