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.

João Farias
11.2k2 gold badges21 silver badges41 bronze badges
asked Jan 20, 2020 at 7:25
4
  • What are you expecting to get? Commented Jan 20, 2020 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) Commented Jan 20, 2020 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. Commented Jan 20, 2020 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. Commented Jan 20, 2020 at 11:03

1 Answer 1

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"));
answered Jan 21, 2020 at 8:13
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. Commented Jan 22, 2020 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? Commented Jan 22, 2020 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 Commented Jan 22, 2020 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. Commented Jan 24, 2020 at 5:28

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.