0

I'm Using RestAssured, and i'm getting the response like this-

 {
        "Data": {
            "Sub": {
                "SubDetails": [
                    {
                        "OrgId": 5,
                        "SubId": 1,
                        "SubName": "Mathematics"
                    }
                ]
            }
        },
        "RawData": {
            "Url": "http://localhost:11111/cases/case-15",
            "Type": "Rest",
            "Request": {
                "Details": {
                    "OrganizationId": 5,
                    "Student": {
                        "Age": 30,
                        "Religion": "Hindu",
                        "StudentId": 10
                    }
                }
            },
            "Response": {
                "SmartReturnObject": {
                    "Subject": [
                        {
                            "SubjectId": 1,
                            "SubjectName": "Mathematics"
                        }
                    ],
                    "OrganizationId": 5
                }
            },
            "IsApiError": false
        },
        "SessionId": "5q0",
        "RequestUniqueId": "4543534",
        "StatusCode": "4540000",
        "StatusMessage": "Success",
        "DataSource": "DD"
    }   

Now i need to extract the Data object separate and RawData separate but I failed.
I tried with JSONPath also but not getting it in JSON format.
I tried

      JsonPath body= response.jsonPath();
      Object value=body.get("Data");
      System.out.println("Value is-"+value);  

and getting it like this - {Sub={SubDetails=[{OrgId=5, SubId=1, SubName=Mathematics}]}}
I want the response as instanceof JSONObject or JSONArray type.Response can be dynamic like JSONObject or JSONArray can be at any place.

4
  • What are you expecting to get? Jan 20 '20 at 9:00
  • I want to get Data object and RawData object separately from response, in instanceof JSONObjector JSONArray type (in whatever format response is) Jan 20 '20 at 9:23
  • Why do you want to do that? if you want to send that response using the next request then you can just reuse the string.
    – PDHide
    Jan 20 '20 at 10:16
  • 1
    Actually, I need to dynamically traverse the complete JSON and then i have to match it with other Expected JSON, so that i can identify that at which placea any Key Value pair or any structure is different from expected one. Jan 20 '20 at 11:03
1

Why is this not an option?

JsonPath resJson= new JsonPath(response);
JsonPath referrenceJson= new JsonPath(jsonTOCompare);
Assert.assertEquals(resJson.get("fields"), referrenceJson.get("fields"));
4
  • Because i want to compare each property, and if any mismatch happen then i need to know that whether key not matched or any value not matched. Also some time i got the scenario like i need to ignore the some property during compare. Jan 22 '20 at 6:26
  • Could you provide a real example of what you tried to achieve but failed? Something that you think it will not work with the suggested solution?
    – PDHide
    Jan 22 '20 at 19:42
  • you can edit the question add something like i wanted to compare {a:1} with [{a:2}] . Tried suggested answer but got this error
    – PDHide
    Jan 22 '20 at 19:46
  • 1
    I need to compare the response json to json coming from .json file, by using your way i can't do that until i will not get the response in instance of JSONObject or JSONArray. Jan 24 '20 at 5:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.