Month: July 2016

Jackson JSON Java Parser Example

Posted on

Jackson JSON Parser API provides easy way to convert JSON to POJO Object and supports easy conversion to Map from JSON data. Jackson supports generics too and directly converts them from JSON to object.

we can add below dependency in your pom.xml.

<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-core -->
<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-core</artifactId>
    <version>2.8.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind -->
<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-databind</artifactId>
    <version>2.8.0</version>
</dependency>

<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-annotations -->
<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-annotations</artifactId>
    <version>2.8.0</version>
</dependency>

jackson-databind jar depends on jackson-core and jackson-annotations libraries, so if you are adding them directly to build path, make sure you add all three otherwise you will get a runtime error.

Below is the Utilities class that uses Jackson API to transform String to Json to Map object or any Java type object or vice-versa.   ThreadLocal is used here because ObjectMapper is not Thread’s safe in Multithreading app otherwise simply create a static variable of ObjectMapper.


package com.example.utilities;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;

import java.io.IOException;
import java.util.Map;

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

    protected static final ThreadLocal OBJECT_MAPPER_THREAD_LOCAL = ThreadLocal.withInitial(() -> new ObjectMapper());

    /**
     * Convert String as JSON to JsonNode.
     *
     * @param payload
     * @return
     */
    public static JsonNode getJsonFromString(String payload) {
        try {
            return OBJECT_MAPPER_THREAD_LOCAL.get().readTree(payload);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * Convert JsonNode Object to Map
     *
     * @param payload
     * @return
     */
    public static Map getMapFromJson(JsonNode payload) {
        try {
            return OBJECT_MAPPER_THREAD_LOCAL.get().readerFor(Map.class).readValue(payload);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * Convert Map Object to JsonNode.
     *
     * @param payload
     * @return
     */
    public static JsonNode getJsonFromMap(Map payload) {
        try {
            return getJsonFromString(OBJECT_MAPPER_THREAD_LOCAL.get().writeValueAsString(payload));
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * Create a Empty Object Node. i.e {}
     *
     * @return
     */
    public static ObjectNode createObjectNode() {
        return OBJECT_MAPPER_THREAD_LOCAL.get().createObjectNode();
    }

    /**
     * Create a Empty Array Json Object i.e [{},{}]
     *
     * @return
     */
    public static ArrayNode createArrayNode() {
        return OBJECT_MAPPER_THREAD_LOCAL.get().createArrayNode();
    }

    /**
     * Remove particular key elements from JsonNode.
     *
     * @param payload
     * @param key
     * @return
     */
    public static JsonNode remove(JsonNode payload, String key) {
        ((ObjectNode) payload).remove(key);
        return payload;
    }

    public static void main(String[] args) {
        String payload = "{\"id\":\"1001\", \"amount\":\"10000045.999\", \"currency\":\"EUR\"}";
        System.out.println(" Payload String  >> " + payload);
        JsonNode jsonNode = getJsonFromString(payload);
        System.out.println(" Convert String Payload to JsonNode  >> " + payload);
        Map map = getMapFromJson(getJsonFromString(payload));
        System.out.println(" GET Map Object from Payload String >> " + map);
        System.out.println(" Before Removing 'amount' from payload >> " + jsonNode);
        remove(jsonNode, "amount");
        System.out.println(" After Removing 'amount' from payload >> " + jsonNode);
        System.out.println(" Convert Map to JSON Node Object  >>" + getJsonFromMap(map));

        System.out.println(" Employee JsonNode >>" + createEmployeeJson());
        System.out.println(" Convert Employee Json to Map Object >>" + getMapFromJson(createEmployeeJson()));
    }

    /**
     * Testing on some real data.
     * employee.json
     * {
     * "id":"123",
     * "name":"Rahul Kumar",
     * "address":{
     *      "street":"Kalkaji",
     *      "city":"Delhi",
     *      "zipcode":"110020"
     * },
     * "phoneNumbers":[
     *      "9923456469",
     *      "9886786786"
     * ],
     * "role":"Developer"
     * }
     *
     * @return
     */
    private static JsonNode createEmployeeJson() {
        ObjectNode employeeJsonNode = createObjectNode();
        ObjectNode addressNode = createObjectNode();
        ArrayNode phoneList = createArrayNode();
        addressNode.put("street", "Kalkaji");
        addressNode.put("city", "Delhi");
        addressNode.put("zipcode", "110020");

        phoneList.add("9923456469");
        phoneList.add("9886786786");

        employeeJsonNode.put("id", "123");
        employeeJsonNode.put("name", "Rahul Kumar");
        employeeJsonNode.set("address", addressNode);
        employeeJsonNode.set("phoneNumbers", phoneList);
        employeeJsonNode.put("role", "Developer");
        return employeeJsonNode;
    }
}


output:-

Payload String >> {"id":"1001", "amount":"10000045.999", "currency":"EUR"}
 Convert String Payload to JsonNode >> {"id":"1001", "amount":"10000045.999", "currency":"EUR"}
 GET Map Object from Payload String >> {id=1001, amount=10000045.999, currency=EUR}
 Before Removing 'amount' from payload >> {"id":"1001","amount":"10000045.999","currency":"EUR"}
 After Removing 'amount' from payload >> {"id":"1001","currency":"EUR"}
 Convert Map to JSON Node Object >>{"id":"1001","amount":"10000045.999","currency":"EUR"}
 Employee JsonNode >>{"id":"123","name":"Rahul Kumar","address":{"street":"Kalkaji","city":"Delhi","zipcode":"110020"},"phoneNumbers":["9923456469","9886786786"],"role":"Developer"}
 Convert Employee Json to Map Object >>{id=123, name=Rahul Kumar, address={street=Kalkaji, city=Delhi, zipcode=110020}, phoneNumbers=[9923456469, 9886786786], role=Developer}
Advertisements

How to read CSV file in Java

Posted on

Jackson data format module for reading and writing CSV-encoded data, either as “raw” data (sequence of String arrays ) , or via data binding to/from Java Objects (POJOs).

To use this extension on Maven-based projects, use following dependency:

<dependency>
  <groupId>com.fasterxml.jackson.dataformat</groupId>
  <artifactId>jackson-dataformat-csv</artifactId>
  <version>2.7.0</version>
</dependency>

Read CSV file based on Java Class.

AS below schema Java Object have explicitly defined Order and properties type to ensured that reading or writing the data in given order.


package com.example.model;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;
import com.fasterxml.jackson.annotation.JsonRootName;

import java.io.Serializable;
import java.math.BigDecimal;

/**
 * Created by MYPC on 7/18/2016.
 */
@JsonPropertyOrder(value = {"id", "amount", "currency"})
@JsonRootName("transaction")
public class Transaction implements Serializable {
    public enum Currency {
        INR, HKD, EUR, USD
    }
    @JsonProperty
    private String id;
    @JsonProperty
    @JsonFormat(shape = JsonFormat.Shape.NUMBER)
    private BigDecimal amount;
    @JsonProperty
    private Currency currency;

    public String getId() {
        return id;
    }
    public void setId(String id) {
        this.id = id;
    }
    public double getAmount() {
        return amount.doubleValue();
    }
    public void setAmount(BigDecimal amount) {
        this.amount = amount;
    }
    public Currency getCurrency() {
        return currency;
    }
    public void setCurrency(Currency currency) {
        this.currency = currency;
    }
    @Override
    public String toString() {
        final StringBuffer sb = new StringBuffer("Transaction{");
        sb.append("id='").append(id).append('\'');
        sb.append(", amount=").append(amount);
        sb.append(", currency=").append(currency);
        sb.append('}');
        return sb.toString();
    }
}


Trade.csv  file reside in project “/src/main/resources” folder.

id,amount,currency
"1001",10000045.999,"EUR"
"1002",17777045.999,"HKD"
"1003",10000045.999,"USD"
"1004",1234045.999,"EUR"
"1005",100875.999,"HKD"
"1006",165780045.999,"EUR"
"1007",32452345.999,"INR"
"1008",7777777.999,"EUR"

 

Reading CSV file to Java Object.


package com.example;

import com.example.model.Transaction;
import com.fasterxml.jackson.dataformat.csv.CsvMapper;
import com.fasterxml.jackson.dataformat.csv.CsvSchema;

import java.io.IOException;
import java.util.List;
/**
 * Created by MYPC on 7/18/2016.
 */
public class JSONSchemaReaderApp {

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

        CsvMapper csvMapper = new CsvMapper();
        CsvSchema csvSchema = csvMapper.typedSchemaFor(Transaction.class).withHeader();
        List list = new CsvMapper().readerFor(Transaction.class)
                .with(csvSchema.withColumnSeparator(CsvSchema.DEFAULT_COLUMN_SEPARATOR))
                .readValues(JSONSchemaReaderApp.class.getClassLoader().getResource("trade.csv"))
                .readAll();
        for (int i = 0; i < list.size(); i++) {
            System.out.printf(" Transaction Row [%d] : %s \n", i + 1, list.get(i));
        }
    }
}


Output:-

Transaction Row [1] : Transaction{id='1001', amount=10000045.999, currency=EUR}
Transaction Row [2] : Transaction{id='1002', amount=17777045.999, currency=HKD}
Transaction Row [3] : Transaction{id='1003', amount=10000045.999, currency=USD}
Transaction Row [4] : Transaction{id='1004', amount=1234045.999, currency=EUR}
Transaction Row [5] : Transaction{id='1005', amount=100875.999, currency=HKD}
Transaction Row [6] : Transaction{id='1006', amount=165780045.999, currency=EUR}
Transaction Row [7] : Transaction{id='1007', amount=32452345.999, currency=INR}
Transaction Row [8] : Transaction{id='1008', amount=7777777.999, currency=EUR}

Reading CSV file into Map


public static void main(String[] args) throws IOException {
        CsvMapper csvMapper = new CsvMapper();

        CsvSchema csvSchema = csvMapper.typedSchemaFor(Map.class).withHeader();
        MappingIterator<Map<String, String>> it = csvMapper.readerFor(Map.class).with(csvSchema.withColumnSeparator(CsvSchema.DEFAULT_COLUMN_SEPARATOR))
                .readValues(JSONSchemaReaderApp.class.getClassLoader().getResource("trade.csv"));
        List<Map<String, String>> listOfMapSchema = it.readAll();

        for (Map<String, String> map : listOfMapSchema) {
            System.out.println(map);
        }

    }

output:-

{id=1001, amount=10000045.999, currency=EUR}
{id=1002, amount=17777045.999, currency=HKD}
{id=1003, amount=10000045.999, currency=USD}
{id=1004, amount=1234045.999, currency=EUR}
{id=1005, amount=100875.999, currency=HKD}
{id=1006, amount=165780045.999, currency=EUR}
{id=1007, amount=32452345.999, currency=INR}
{id=1008, amount=7777777.999, currency=EUR}