0

I have a below response which I am unable to validate due to response is starting from JSON object 0. Thats means if more than one object, the response will start from 0 to the number of objects you have.

I have tried this but its not working and ends up with stack over flow error.

public static void Location_ContactValidation(Response response, String code, String message, ExtentTest log) {
 try {
  softAssert = new SoftAssert();
  org.json.JSONObject jsonObject = new org.json.JSONObject(response);
  org.json.JSONObject getSth = jsonObject.getJSONObject("status");
  status_Message = getSth.get("message");
  softAssert.assertEquals(code, code);
  softAssert.assertEquals(message, status_Message);
  log.log(LogStatus.INFO, "Validation: The status code is " + code);
  log.log(LogStatus.INFO, "Validation: The status message is " + status_Message.toString());

 } catch (Exception e) {
  log.log(LogStatus.INFO, "Validation: The status code is " + code);
  if (status_Message != null) {
   log.log(LogStatus.INFO, "Validation: The status message is " + status_Message.toString());
  }
  System.out.println(e.getMessage());
  if (softAssert != null) {
   softAssert.assertAll();
  }
 }
}

Stack overflow error as flows-

java.lang.StackOverflowError
    at org.json.JSONObject.wrap(JSONObject.java:1746)
    at org.json.JSONArray.<init>(JSONArray.java:176)
    at org.json.JSONObject.wrap(JSONObject.java:1747)
    at org.json.JSONObject.populateMap(JSONObject.java:1167)

And here is the response I want to parse

{
    "0": {
        "status": "OK",
        "data": {
            "id": "*************",
            "mobile": "*************"
        },
        "message": "Submitted Successfully"
    },
    "status": "OK"
}

I need to validate the mobile number, both the status and message. But not able to do it. If one more number is send with request then the response increases and to array gets created first as shown with 0 then with 1.

I appreciate your help.

1 Answer 1

1

You can list all keys for given JSONObject using keySet method. After that you can directly dig into required fields.

Below codes shows how to read all required fields:

import org.json.JSONObject;

import java.io.File;
import java.nio.file.Files;

public class OrgJsonApp {

    public static void main(String[] args) throws Exception {
        File jsonFile = new File("./resource/test.json").getAbsoluteFile();
        String json = String.join("", Files.readAllLines(jsonFile.toPath()));

        JSONObject response = new JSONObject(json);
        response.keySet().forEach(key -> {
            JSONObject object = response.optJSONObject(key);
            // if given key represents object it must be data
            if (object != null) {
                final String dataKey = "data";
                JSONObject data = object.optJSONObject(dataKey);
                // extract mobile from data and remove data
                // this way JSON node is much simpler
                if (data != null) {
                    final String mobileKey = "mobile";
                    String mobile = data.optString(mobileKey);
                    System.out.println("Mobile => " + mobile);
                }
                System.out.println("status => " + object.optString("status"));
                System.out.println("message => " + object.optString("message"));
            }
        });
    }
}

For below JSON payload:

{
  "0": {
    "status": "OK",
    "data": {
      "id": "1",
      "mobile": "44-32-12"
    },
    "message": "Submitted Successfully"
  },
  "1": {
    "status": "OK",
    "data": {
      "id": "2",
      "mobile": "9981234-543"
    },
    "message": "Submitted Successfully"
  },
  "status": "OK"
}

prints:

Mobile => 44-32-12
status => OK
message => Submitted Successfully
Mobile => 9981234-543
status => OK
message => Submitted Successfully
Sign up to request clarification or add additional context in comments.

3 Comments

But I dont want to create json array of my object keys
Can we not just print single single keys so validation becomes easy?
@SobhitSharma, check my update. It prints single keys which should be easy to validate now.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.