Java

Parsing a JSON Object and extracting values from it

This blog post we will try to parse a JSONObject and fetch the values from it.

Consider the following JSON String

{
     "address": {
          "street": "Street 1",
          "landmark": "Next to Bus Stop",
          "lane": "Lane 5",
          "building": "Building XYZ"
     },
     "orderId": 1,
     "custName": "Shaun",
     "deliveryDate": "2020-01-05",
     "orderDate": "2020-01-02",
     "cart": [
          {
               "productId": 1,
               "price": 23,
               "qty": 2
          },
          {
               "productId": 4,
               "price": 32,
               "qty": 4
          }
     ]
}

It contains the following things

  1. An internal JSONObject by name of "address" which internally contains name/value pairs by name of "street", "landmark", "lane", "building"
  2. A JSONArray by name of "cart" which contains order items containing name/value pairs by name of "productId", "price", "qty"
  3. Name/Value pairs by name of "orderId", "custName", "deliveryDate", "orderDate"   
  4. Using getter methods of JSONObject you can fetch the which are inside it

Following is the list of getter methods

Now using the combination we will write a simple program

import org.json.*;


public class ParsingAJSON
{
    public static void main(String[] args)
    {
        String jsonString="{orderId:1,custName:Shaun,orderDate:2020-01-02,deliveryDate:2020-01-05,cart:[{productId:1,qty:2,price:23},{productId:4,qty:4,price:32}],address:{street:Street 1,lane:Lane 5,building: Building XYZ,landmark:Next to Bus Stop}}";
       
        JSONObject obj=new JSONObject(jsonString);
        System.out.println("JSON String:\n\n"+obj.toString(5)+"\n\n-----------------------------------------");


        int orderId=obj.getInt("orderId");
        System.out.println("Order Id:"+orderId);
        String custName=obj.getString("custName");
        System.out.println("Customer Name:"+custName);
        String orderDate=obj.getString("orderDate");
        System.out.println("Order Date:"+orderDate);
        String deliveryDate=obj.getString("deliveryDate");
        System.out.println("Delivery Date:"+deliveryDate);


        System.out.println("\n\n------------------Iterating Cart Item------------------\n\n");


        JSONArray array=obj.getJSONArray("cart");
        
        for(int i=0;i<array.length();i++)
        {
            JSONObject cartObject=array.getJSONObject(i);


            int productId=cartObject.getInt("productId");
            int qty=cartObject.getInt("qty");
            int price=cartObject.getInt("price");


            System.out.println("Product Id:"+productId+" | Qty:"+qty+" | Price:"+price);


        }


        System.out.println("\n\n-------------- Parsing JSON Object inside the JSON Object --------------\n\n");


        JSONObject address=obj.getJSONObject("address");


        String street=address.getString("street");
        String lane=address.getString("lane");
        String building=address.getString("building");
        String landmark=address.getString("landmark");


        System.out.println("Address:- "+street+", "+lane+", "+building+", "+landmark);


    }


}
Output:
-----------------------------------------
Order Id:1
Customer Name:Shaun
Order Date:2020-01-02
Delivery Date:2020-01-05

------------------Iterating Cart Item------------------

Product Id:1 | Qty:2 | Price:23
Product Id:4 | Qty:4 | Price:32

-------------- Parsing JSON Object inside the JSON Object --------------

Address:- Street 1, Lane 5, Building XYZ, Next to Bus Stop

This is how you parse a JSONObject to fetch the values from it.

 

Related Posts

language_img
Java

What is JSON

Jun 11, 2020

Table Of Contents

;