4

Need help to validate particular JSON parameter from below response using REST Assured library.

I tried some of the options to validate other similar parameter as shown below but it didn't work.

.then().body("value.value.value.name", hasItems("balanceResultCode")).body("value.value.value.value", hasItems("0"));

If I want to validate "resultCode" & "subscriberIdType" from below response then how can I do it?

[  
{  
    "name":"Id",
    "value":"11"
},
{  
    "name":"version",
    "value":null
},
{  
    "name":"header",
    "value":[  
        {  
            "name":"mVersion",
            "value":"1"
        },
        {  
            "name":"Name",
            "value":"BalQ"
        },
        {  
            "name":"appID",
            "value":"90091"
        },
        {  
            "name":"requestUid",
            "value":"REST_REQUQEST_1"
        },
        {  
            "name":"sessionId",
            "value":"REST_SESSION_1"
        },
        {  
            "name":"requestType",
            "value":"SomeRequestType"
        },
        {  
            "name":"requestNumber",
            "value":"REQ_111"
        },
        {  
            "name":"requestDuplicate",
            "value":"1"
        },
        {  
            "name":"serviceProvider",
            "value":1
        },
        {  
            "name":"username",
            "value":"user"
        },
        {  
            "name":"password",
            "value":"pass"
        },
        {  
            "name":"resultCode",
            "value":100
        }
    ]
},
{  
    "name":"content",
    "value":[  
        {  
            "name":"subscriberAddressing",
            "value":[  
                {  
                    "name":"subscriber",
                    "value":[  
                        {  
                            "name":"subscriberIdType",
                            "value":200
                        },
                        {  
                            "name":"subscriberIdValue",
                            "value":"1234567890"
                        }
                    ]
                }
            ]
        }
    ]
}

]

2 Answers 2

5

This is one way:

then().
       body("find { it.name == 'header' }.value.find { it.name == 'resultCode' }.value", is(100)).
       body("find { it.name == 'content' }.value.find { it.name == 'subscriberAddressing' }.value.find { it.name == 'subscriber' }.value.find { it.name == 'subscriberIdType'}.value", is(200));

You can read up on Groovy collections and GPath to learn more.

Sign up to request clarification or add additional context in comments.

3 Comments

thank you for your prompt response! It really worked. I have only once concern, will there be any performance impact if I have very big response with many such generic parameters, as we are using "FIND" at each node level ?
Also, if you have any documents or useful links related to Groovy collections and GPath then please share. thx
Please also share if there is any other way, as I see you have mentioned above that This is one way... I would be happy to know all other ways as well for my knowledge. Thx! :)
0

First of all you need to import

import static org.hamcrest.CoreMatchers.equalTo;

after that you may use it as follows

RestAssured.given().auth().oauth2(token).when().accept("application/json").get("someurl").then().statusCode(200).body("name[0]", equalTo("Anwar"));

Comments

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.