- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Convert CSV to JSON using the Jackson library in Java?n
A Jackson is a Java JSON API that provides several different ways to work with JSON. We can convert CSV data to JSON data using the CsvMapper class, it is specialized ObjectMapper, with extended functionality to produce CsvSchema instances out of POJOs. We can use the reader() method for constructing ObjectReader with default settings. In order to convert this, we need to import the com.fasterxml.jackson.dataformat.csv package.
In the below example, convert a CSV to JSON.
Example
import java.io.*; import java.util.*; import com.fasterxml.jackson.databind.*; import com.fasterxml.jackson.dataformat.csv.*; public class CsvToJsonTest { public static void main(String args[]) throws Exception { File input = new File("input.csv"); try { CsvSchema csv = CsvSchema.emptySchema().withHeader(); CsvMapper csvMapper = new CsvMapper(); MappingIterator<Map<?, ?>> mappingIterator = csvMapper.reader().forType(Map.class).with(csv).readValues(input); List<Map<?, ?>> list = mappingIterator.readAll(); System.out.println(list); } catch(Exception e) { e.printStackTrace(); } } }
Output
[{last name=Chandra, first name=Ravi, location=Bangalore}]
- Related Articles
- Convert CSV to JSON using the Jackson library in Java?
- Convert JSON to/from Map using Jackson library in Java?
- How to convert Java object to JSON using Jackson library?
- How to convert a JSON to Java Object using the Jackson library in Java?
- How to convert a List to JSON array using the Jackson library in Java?
- Pretty print JSON using Jackson library in Java?
- Convert POJO to XML using the Jackson library in Java?
- Convert XML to POJO using the Jackson library in Java?
- How to ignore a field of JSON object using the Jackson library in Java?
- Convert Java object to JSON using the Gson library in Java?
- Convert a JSON String to Java Object using the json-simple library in Java?
- Convert a Map to JSON using the Gson library in Java?
- Convert JSON object to Java object using Gson library in Java?
- How to convert a JSON object to an enum using Jackson in Java?
- How to convert Java object to JSON using GSON library?
- How to convert a JSON array to CSV in Java?

Advertisements