Month: August 2016

How to read/write CSV file to Map in java

Posted on Updated on

As you see in last article, how to read a CSV file into Java object with the help of Jackson API. In this article, we will see how to write a list of Map ( List<Map<String,String>> ) into a CSV file in your machine and again read it through with Jackson API.

Add this dependency in your pom.xml file to used Jackson API in your application.



Below are a sample example of reading or writing a CSV file with the help of Jackson API, which makes it very easy to write or read the CSV file into Java Object.


package com.example;

import com.fasterxml.jackson.dataformat.csv.CsvMapper;
import com.fasterxml.jackson.dataformat.csv.CsvSchema;

import java.util.*;

 * Created by MYPC on 8/1/2016.
public class ConvertMapToCSVTest {
    public static void main(String[] args) throws IOException {

        List<HashMap<String, String>> myArrList = new ArrayList<HashMap<String, String>>();
        HashMap<String, String> map;

        /*** Rows 1 ***/
        map = new HashMap<String, String>();
        map.put("CustomerID", "C001");
        map.put("Name", "Ajay Kumar");
        map.put("Email", "");
        map.put("CountryCode", "TH");
        map.put("Budget", "1000000");
        map.put("Used", "600000");

        /*** Rows 2 ***/
        map = new HashMap<String, String>();
        map.put("CustomerID", "C002");
        map.put("Name", "Rahul Kumar");
        map.put("Email", "");
        map.put("CountryCode", "UK");
        map.put("Budget", "2000000");
        map.put("Used", "800000");

        /*** Rows 3 ***/
        map = new HashMap<String, String>();
        map.put("CustomerID", "C003");
        map.put("Name", "Wasim Akram");
        map.put("Email", "");
        map.put("CountryCode", "US");
        map.put("Budget", "3000000");
        map.put("Used", "600000");

        /*** Rows 4 ***/
        map = new HashMap<String, String>();
        map.put("CustomerID", "C004");
        map.put("Name", "Santosh Kumar");
        map.put("Email", "");
        map.put("CountryCode", "US");
        map.put("Budget", "4000000");
        map.put("Used", "100000");

        File file = new File("d:\\sample.csv");
        // Create a File and append if it already exists.
        Writer writer = new FileWriter(file, true);
        Reader reader = new FileReader(file);
        //Copy List of Map Object into CSV format at specified File location.
        csvWriter(myArrList, writer);
        //Read CSV format from specified File location and print on console..

     * @param listOfMap
     * @param writer
     * @throws IOException
    public static void csvWriter(List<HashMap<String, String>> listOfMap, Writer writer) throws IOException {
        CsvSchema schema = null;
        CsvSchema.Builder schemaBuilder = CsvSchema.builder();
        if (listOfMap != null && !listOfMap.isEmpty()) {
            for (String col : listOfMap.get(0).keySet()) {
            schema ="\r").withHeader();
        CsvMapper mapper = new CsvMapper();

     * @param collection
     * @param writer
     * @param 
     * @throws IOException
    public static  void csvWriter(Collection collection, Writer writer) throws IOException {
        if (collection != null && collection.size() > 0) {
            CsvMapper mapper = new CsvMapper();
            Object[] objects = collection.toArray();
            Class type = objects[0].getClass();
            CsvSchema schema = mapper.schemaFor(type).withHeader();
        } else {
            writer.write("No Data");

     * @param reader
     * @throws IOException

    public static void csvReader(Reader reader) throws IOException {
        Iterator<Map<String, String>> iterator = new CsvMapper()
        while (iterator.hasNext()) {
            Map<String, String> keyVals =;

A CSV file is created at specified location i.e d:\\sample.csv in file system.

Email,Used,Budget,CustomerID,CountryCode,Name,600000,1000000,C001,TH,"Ajay Kumar",800000,2000000,C002,UK,"Rahul Kumar",600000,3000000,C003,US,"Wasim Akram",100000,4000000,C004,US,"Santosh Kumar"

Reading sample.csv file from file system.

{, Used=600000, Budget=1000000, CustomerID=C001, CountryCode=TH, Name=Ajay Kumar}
{, Used=800000, Budget=2000000, CustomerID=C002, CountryCode=UK, Name=Rahul Kumar}
{, Used=600000, Budget=3000000, CustomerID=C003, CountryCode=US, Name=Wasim Akram}
{, Used=100000, Budget=4000000, CustomerID=C004, CountryCode=US, Name=Santosh Kumar}

Hoping, It would be helpful !! Thanks

JSON Schema Validation example in java

Posted on

Validating input is a good programming practice than spending times to trying to guard each line of codes against all the way the input goes wrong.

This is especially true when we are writing a web service that deal with input data of a PUT,POST,DELETE and GET method with MIME type JavaScript Object Notation (JSON) .  JSON data is formatted as a key & value pair and each key & value represent a properties name and value separated by the colon (:).

In last post, we have seen the Jackson framework that is used to marshaling and unmarshalling the JSON data to Java object and vice-versa, But validating JSON data is another task, unfortunately Jackson framework does not  provide to do validate JSON data.

json-schema-validator available at is quite popular with Java developer to validate JSON data against Schema file.

JSON schema is:

  • A way of describing your existing data format
  • Written in clear, human and machine readable, documentation
  • Complete structural validation useful for
    • Automated testing
    • Validating client submitted data

Look at the below Schema file. This schema file exactly tells, in Input JSON data , “id” must be String type, “amount” properties must be any Number type and “Currency” must be out of four currency else throw validation exception. As you can created own Schema file base on the incoming input date.


trade-schema.json under /src/main/resources/schema folder

  "title": "Daily Trade Transaction",
  "description": "Daily Trade Transaction",
  "type": "object",
  "properties": {
      "enum":["INR", "HKD", "EUR", "USD"]

ref link:-
validate Schema online :-

Let’s build a simple Application :-

trade.json :- Input data store in a file.



JsonSchemaManager.Java : Used to validate all Java Object with specific Schema.

package com.example.utilities;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.RuntimeJsonMappingException;
import com.github.fge.jackson.JsonLoader;
import com.github.fge.jsonschema.core.exceptions.ProcessingException;
import com.github.fge.jsonschema.main.JsonSchemaFactory;
import com.github.fge.jsonschema.main.JsonValidator;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static javafx.scene.input.KeyCode.J;

 * Created by MYPC on 7/17/2016.
public class JsonSchemaManager {
    private final JsonValidator validator = JsonSchemaFactory.byDefault().getValidator();
    private Map<Class<?>, JsonNode> jsonNodeMap = new HashMap<>();

    public void load(Class<?> className, String schema) throws IOException {
        JsonNode schemaFromDisk = JsonLoader.fromURL(this.getClass().getResource(schema));
        jsonNodeMap.put(className, schemaFromDisk);

    public void check(Class<?> className, JsonNode toBeValidate) {

        ProcessingReport report = null;
        try {
            report = validator.validate(jsonNodeMap.get(className), toBeValidate);
            if (!report.isSuccess()) {
                StringBuilder stringBuilder = new StringBuilder();
                stringBuilder.append(" Oops!! failed JSON validation ");
                List messages = Lists.newArrayList(report);

                for (int i = 0; i < messages.size(); i++) { stringBuilder.append("- "); stringBuilder.append(messages.get(i).toString()); stringBuilder.append((i == (messages.size()) - 1) ? "" : "\r"); } throw new RuntimeException(stringBuilder.toString()); } } catch (ProcessingException e) { throw new RuntimeJsonMappingException("ERROR -->" + e.toString());

Main Class that validate the JSON data.

package com.example;

import com.example.model.Transaction;
import com.example.service.CSVReader;
import com.example.utilities.JsonSchemaManager;
import com.fasterxml.jackson.databind.JsonNode;

import java.util.List;

 * Created by MYPC on 7/18/2016.
public class JSONSchemaReaderApp {

    public static void main(String[] args) throws IOException {

        JsonSchemaManager jsonSchemaManager = new JsonSchemaManager();
        CSVReader csvReader = new CSVReader();
        // Load schema file from resource folder.
        jsonSchemaManager.load(Transaction.class, "/schema/trade-schema.json");
        // Load JSON data from file system and convert into JsonNode Object
        List lists = csvReader.load(Transaction.class, JSONSchemaReaderApp.class.getClassLoader().getResource("trade.csv"));
        for (int i = 0; i < lists.size(); i++) {
            //validate each JsonNode of Type Transaction to Schema file.
            jsonSchemaManager.check(Transaction.class, lists.get(i));

complete src download here