0

I'm trying to loop the calls: JSON array and trying to fetch the machine details JSON object which is present under calls JSON array list as like below:

{
   "<dynamicValue>":{
      "type":"CORR-ID",
      "tags":[
         {
            "name":"9VB6454145983212H",
            "flags":[
               "FLAG_DYNAMIC_VALUE",
               "FLAG_ID_LOOKUP_SUPPORTED"
            ]
         }
      ],
      "callSummary":[
         {
            "colo":"lvs",
            "pool":"amazon_paymentsplatformserv",
            "machine":"stage2utb29958"
         },
         {
            "colo":"lvs",
            "pool":"amazon_elmoserv",
            "machine":"msmamoserv_0"

         },
         {
            "colo":"lvs",
            "pool":"amazon_xopaymentgatewayserv",
            "machine":"msmastmentgatewayserv_1"
         },
         {
            "colo":"lvs",
            "pool":"amazon_paymentapiplatserv",
            "machine":"msmaentapiplatserv_2"
         },
         {
            "colo":"lvs",
            "pool":"amazon_userlifecycleserv_ca",
            "machine":"stage2utb91581"
         },
         {
            "colo":"lvs",
            "pool":"amazon_dafproxyserv",
            "machine":"msmasfproxyserv_1"
         },
         {
            "colo":"lvs",
            "pool":"paymentserv",
            "machine":"te-alm-15757_paymentexecutionserv_0",
            "calls":[
               {
                  "colo":"lvs",
                  "pool":"fimanagementserv_ca",
                  "machine":"msmgementserv_ca_20"
               },
               {
                  "colo":"lvs",
                  "pool":"fimanagementserv_ca",
                  "machine":"msmasgementserv_ca_4"
               }
               ]
              }
            ]
    }
}

The above JSON file which I stored in String variable and trying to fetch the machine details which is under calls: JSON ARRAY by using below code.

Code:

    public static void getHttpUrlformachineList(String response, String CalId, String componentName)
            throws Exception
      {
           //System.out.println(response);
           Map<String, String> data = new HashMap<String, String>();
           JSONParser parser = new JSONParser();
           JSONObject object = (JSONObject) parser.parse(response);
           JSONObject getValue = (JSONObject) object.get(CalId.trim()); //CalId is the dynamic value that mentioned in the JSON input file
           JSONObject getCalSummary = (JSONObject) object.get("callSummary");
           JSONArray arrays=(JSONArray) getCalSummary.get("calls");
           System.out.println(arrays.size()); // return null pointer
       }

Error:

    java.lang.NullPointerException: null
        at com.online.amazon.hadoop.cal.swagger.utils.Utils.getHttpUrlformachineList(Utils.java:112) ~[classes/:na]

If you notice that calls Array List will not be available in all the callSummary JSON Array, and It will be dynamic and can be available under any component that listed above.

So I just want to dynamically get the calls: JSON array and iterate and fetch machine details.

Can someone help me to achieve this?

Note: I'm using JSON-Simple library to parse and iterate the JSON. It would be great if I get solution on the same.

Updated:

I also tried to create callSummary as JSON array and loop that array to get each JSON object and tried to find the calls but this is also leads to Null pointer.

Also the calls json array is not index specific. It can be anywhere in the payload. It may or may not be there in the payload. I just need to handle if it's exist in any of the component then I need to fetch that machine details

ArrchanaMohan
  • 2,314
  • 4
  • 36
  • 84
  • Your callSummary is of `JSONArray` type and not `JSONObject`. And it seems that your `calls` array only exists in the last element of your `callSummary`. – Christilyn Arjona Dec 18 '19 at 22:25
  • I tried to create callSummary as JSONArray and loop and tried to get each JSON Object but no luck. calls array will be available anywhere in that payload. this is the sample one. calls array is not index based. I just gave one sample. – ArrchanaMohan Dec 18 '19 at 22:27

2 Answers2

1

change JSONArray arrays=(JSONArray) getCalSummary.get("calls");
to
JSONArray arrays= getCalSummary.getJSONArray("calls") and all other functions where you get objects instead of "get" you should use "getJSONObject", "getString" etc.. then you dont have to cast,

also im pretty sure its not arrays.size() its arrays.length() if you are using package org.json.JSONArray but since key "calls" doesnt exist in every "callSummary" you should check if its null or not before.

Herman Jansson
  • 164
  • 1
  • 8
0

You should match the types as specified in your JSON string:

 public static void getHttpUrlformachineList(String response, String CalId, String componentName)
            throws Exception
      {
           //System.out.println(response);
           Map<String, String> data = new HashMap<String, String>();
           JSONParser parser = new JSONParser();
           JSONObject object = (JSONObject) parser.parse(response);
           JSONObject getValue = (JSONObject) object.get(CalId.trim()); //CalId is the dynamic value that mentioned in the JSON input file
           JSONArray getCalSummary = (JSONArray) object.get("callSummary"); // callSummary is a JSONArray, not JSONObject
           for (int i = 0; i < getCalSummary.length(); i++) {
               JSONObject obj = getCalSummary.getJSONObject(i);
               if (obj.has("calls")) {
                   // grab calls array:
                   JSONArray callsArray = obj.getJSONArray("calls");
               }
           }
       }

Here, you should also check your JSON values with .has(...) method to avoid getting JSONException if a field doesn't exists in your JSONObject.

Christilyn Arjona
  • 2,173
  • 3
  • 13
  • 20
  • I don't find the getJSONObject() in json-simple library. – ArrchanaMohan Dec 18 '19 at 22:42
  • @ArrchanaMohan if that's the case. You can just cast it to `JSONObject` as you did in the previous parts – Christilyn Arjona Dec 18 '19 at 22:44
  • 1
    Sure added the below lines and gonna check the result: JSONObject getName = (JSONObject) getCalSummary.get(i); { if(getName .get("calls")!=null) { JSONArray arrays=(JSONArray)getName .get("calls"); System.out.println(arrays.size()); } } – ArrchanaMohan Dec 18 '19 at 22:46
  • 1
    If possible, you should use `has(...)` method: `getName.has("calls")` to check if the JSONObject contains a field called `calls`. I've edited my answer to account for the change. Because you could get a `JSONException` if you call the `get` method on a field that doesn't exists. So it's better to use the built in method `has(...)` – Christilyn Arjona Dec 18 '19 at 22:49
  • The above code working fine now... Sure I will implement the same.. Thank you so much for your help and patience. – ArrchanaMohan Dec 18 '19 at 22:56
  • @ArrchanaMohan Great! Happy to help :D – Christilyn Arjona Dec 18 '19 at 22:57