JSON Tree Model handling with Jackson: Transform JSON string to JSON Tree and access fields

JSON (Employee details):
{
    "payload": {
        "audit": {
            "enteredBy": "NIKHIL",
            "entryType": "ORDER"
        },
        "employees": {
            "employee": [
                {
                    "lineNo": 1,
                    "name": "NIKHIL RANJAN",
                    "email": "NIKHILRANJAN@DEVINLINE.COM",
                    "address": {
                        "firstLine":"WINDSOR APT",
                        "SecondLine":"#204, BLOCK 2",
                        "state":"KARTA",
                        "postalCode":"50081",
                        "phone": {
                            "home": "4747521304",
                            "work": "9748767304"
                        }
                    },
                    "org": "IT",
                    "department": "DPC"
                }
            ],
            "requestType": "EMPLOYEE_DETAILS"
        }
    }
}

Java Sample code:
import java.io.IOException;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.JsonNode;

public class JacksonJsonNodeParser {
 private static com.fasterxml.jackson.databind.ObjectMapper getJacksonMapper() {
  com.fasterxml.jackson.databind.ObjectMapper mapper = 
    new com.fasterxml.jackson.databind.ObjectMapper();
  mapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
  return mapper;
 }

 private static void doJsonTreeParse(String employeeJSON) throws IOException {
  JsonNode responseJsonNode = getJacksonMapper().readTree(employeeJSON);
  JsonNode employeesDetails = responseJsonNode.get("payload").get("employeesDetails");
  JsonNode employees = employeesDetails.get("employees");
  String status = employeesDetails.get("requestType").asText();
  System.out.println(status);
  if (employees.isArray()) {
   for (final JsonNode employee : employees) {
    String name = employee.get("name").asText();
    String email = employee.get("email").asText();
    JsonNode address = employee.get("address");
    JsonNode phone = address.get("phone");
    String homePhone = phone.get("home").asText();
    System.out.println("*** Employee Detail ***");
    System.out.println("Name: " + name);
    System.out.println("Email: " + email);
    System.out.println("HomePhone: " + homePhone);

   }
  }
 }

 public static void main(String[] args) throws JsonProcessingException, IOException {
  String employeeJSON = "{\n" + 
    "    \"payload\": {\n" + 
    "        \"audit\": {\n" + 
    "            \"enteredBy\": \"NIKHIL\",\n" + 
    "            \"entryType\": \"ORDER\"\n" + 
    "        },\n" + 
    "        \"employeesDetails\": {\n" + 
    "            \"employees\": [\n" + 
    "                {\n" + 
    "                    \"lineNo\": 1,\n" + 
    "                    \"name\": \"NIKHIL RANJAN\",\n" + 
    "                    \"email\": \"NIKHILRANJAN@DEVINLINE.COM\",\n" + 
    "                    \"address\": {\n" + 
    "                        \"firstLine\":\"WINDSOR APT\",\n" + 
    "                        \"SecondLine\":\"#204, BLOCK 2\",\n" + 
    "                        \"state\":\"KARTA\",\n" + 
    "                        \"postalCode\":\"50081\",\n" + 
    "                        \"phone\": {\n" + 
    "                            \"home\": \"4747521304\",\n" + 
    "                            \"work\": \"9748767304\"\n" + 
    "                        }\n" + 
    "                    },\n" + 
    "                    \"org\": \"IT\",\n" + 
    "                    \"department\": \"DPC\"\n" + 
    "                }\n" + 
    "            ],\n" + 
    "            \"requestType\": \"EMPLOYEE_DETAILS\"\n" + 
    "        }\n" + 
    "    }\n" + 
    "}";
  doJsonTreeParse(employeeJSON);
 }
}

Sample output:

EMPLOYEE_DETAILS
*** Employee Detail ***
Name: NIKHIL RANJAN
Email: NIKHILRANJAN@DEVINLINE.COM
HomePhone: 4747521304

2 Comments

Previous Post Next Post