Rest-assured. Is it possible to extract JSONObject/JSONArray from response json?


  • QA Engineer

    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.



  • Why is this not an option?

    JsonPath resJson= new JsonPath(response);
    JsonPath referrenceJson= new JsonPath(jsonTOCompare);
    Assert.assertEquals(resJson.get("fields"), referrenceJson.get("fields"));
    


Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2