cancel
Showing results for 
Search instead for 
Did you mean: 

How to assert array response

Highlighted
Contributor

How to assert array response

[
{
"id" : "1234",
"name":"xyz"
},
"jsonfield1" : {
"jsonfield" : "xyz"
}
}
]

 

I got this response of my API. I dont know how to assert array.

Generally, I store in hashmap

 

def response_string = messageExchange.response.responseContent

def response_slurper = new JsonSlurper()
def response_hashmap = response_slurper.parseText(response_string)

assert response_hashmap.id =="123"

Its failing here since its an array. 

 

Looking for a snippet.

4 REPLIES 4
Highlighted
Contributor

Re: How to assert array response

Hello,

 

I would supply a snippet, but i cannot validate the json you presented in your sample...  the json editors I dropped your json into all returned a parse error.  The json sluper in SoapUI could not parse either.  Did you hand code the example or cut and paste directly from API response?  I cannot reproduce with your supplied sample.

 

Regards.

Highlighted
Contributor

Re: How to assert array response

Hi Todd,

 

This is the correct JSON

[{
"id": "1234",
"name": "xyz",
"jsonfield1": {
"jsonfield": "xyz"
}
}]

Highlighted
Contributor

Re: How to assert array response

Hello henil_shah,

 

I think just adding an element number of the array/list to the assertion will work for you.  Groovy script sample below.

 

Regards

import groovy.json.*;

def jsonSampletxt = '''
[{
"id": "1234",
"name": "xyz",
"jsonfield1": {
"jsonfield": "xyz"
}
}]
''';

def jsonSampleobj = new JsonSlurper().parseText(jsonSampletxt);
jsonSampleobj.each { item ->
   log.info 'item=' + item.toString();
};
log.info 'jsonSampleobj.size()= ' + jsonSampleobj.size();
log.info ' ';
log.info 'jsonSampleobj.id=' + jsonSampleobj.id;
log.info 'jsonSampleobj.id[0]=' + jsonSampleobj.id[0];
log.info 'jsonSampleobj.name=' + jsonSampleobj.name;
log.info 'jsonSampleobj.name[0]=' + jsonSampleobj.name[0];

assert jsonSampleobj.id[0] == '1234', "Element 0 of .id Array/List should be equal to '1234'";
assert jsonSampleobj.name[0] == 'xyz', "Element 0 of .name Array/List should be equal to 'xyz'";

log.info 'Test Step "' + testRunner.runContext.currentStep.name + '" done...';
Highlighted
Moderator

Re: How to assert array response

Hello everyone, 

 

@Todd_Neuschwang thanks for the assistance, it is much appreciated!

 

@henil_shah, have you had a chance to try the solution that Todd provided? Does it work for you?

We are looking forward to hearing from you!

 

 


Olga Terentieva
SmartBear Assistant Community Manager

New Here?
Join us and watch the welcome video:
Announcements
TechCorner Leaderboard
Want a fun and easy way to learn ReadyAPI and show off your skills? Try solving weekly TechCorner challenges and get into the Leaderboard!


Challenge Status

Compare an expected JSON value and actual response in Events

Participate!

Fetch value/data from JSON response using Groovy Script

See replies!

Filtering data retrieved from a DataSource

See replies!

Get data from Petstore and add it to Excel sheets

Participate!
Top Kudoed Authors