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
157 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 28, 2019 in Java by anonymous
• 160 points

closed Jan 14 by Kalgi 124 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,080 points
1,042 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
• 46,080 points
91 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 18, 2018 in Java by sharth
• 3,370 points
47 views
0 votes
2 answers

When I am running eclispe on my computer it is throwing error. How to resolve it?

If you have downloaded the 64 bit ...READ MORE

answered Dec 10, 2018 in Java by Sushmita
• 6,890 points
124 views
+15 votes
19 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
• 700 points
106,572 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
• 46,080 points
711 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,150 points
2,517 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
2,232 views