cancel
Showing results for 
Search instead for 
Did you mean: 

Reading nodevalues in Json

Highlighted
Occasional Contributor

Reading nodevalues in Json

How to assert the value of a repeated response element

i'm trying to assert for a specific response element from a REST request. The response can contain up to 20 results within the one one response e.g

"totalCount": 1375,
"results": [
{
"title": "florence italy may 12 ryder ...",

"metadata": {
"caption": "florence italy may 12 ryder ...",
"standard.license": "RF",
"base.thumb_large.width": "100",
"base.thumb_large.height": "150",
"base.thumb_small_width": "67"
}
},
{
"title": "Title number 2...",

"metadata": {
"caption": "florence italy may 12 ryder ...",
"standard.license": "RF",
"base.thumb_large.width": "100",
"base.thumb_large.height": "150",
"base.thumb_small_width": "67"
}
},
{
"title": "Title number 3 ...",

"metadata": {
"caption": "florence italy may 12 ryder ...",
"standard.license": "RF",
"base.thumb_large.width": "100",
"base.thumb_large.height": "150",
"base.thumb_small_width": "67"
}
},

I can assert of the existence of say the first occurrance of caption but as the number of results within the one response can vary, is there any way I can check that caption exists in all of the result elements within the one response?
In other words if the request returns 15 results within the one response, how can I assert that each of those 15 results contains the element 'caption' ?
 I want to use groovy scripting or if there is any other method , I would like to the commands/syntax . My main question is on how to use JsonSlurper for this .
 
3 REPLIES 3
Highlighted
Super Contributor

Re: Reading nodevalues in Json

I was going to suggest groovy with jsonslurper

you are on it.

 

 

Highlighted
Community Hero

Re: Reading nodevalues in Json

erm....don't use jsonSlurper if you've running ReadyAPI! v2.3.0 - if you're still running earlier versions that will work - but I found out today (@New2API told me via another post) that jsonSlurper has been deprecated in the latest version and to use jsonUtil instead

 

Another post on the forum indicated the following changes

 

import net.sf.json.groovy.JsonSlurper

def abc = context.expand(grab an array from request/response)
def abcArray = new JsonSlurper().parseText(abc).results
def x = abcArray.size()

--> x  would tell me the number of nodes


Replaced now with:

import com.eviware.soapui.support.JsonUtil

def abc = context.expand(grab an array from request/response)
def abcArray = JsonUtil.parseTrimmedText(abc)
def x = abcArray.size()

--> x  would tell me the number of nodes

Although - I couldn't get it to jsonUtil to work with my script..... 😞

 

cheers,

 

richie

if this helped answer the post, could you please mark it as 'solved'? Also if you consider whether the title of your post is relevant? Perhaps if the post is solved, it might make sense to update the Subject header field of the post to something more descriptive? This will help people when searching for problems. Ta
Highlighted
Community Hero

Re: Reading nodevalues in Json

Hi,

 

I tested this on a free version of SoapUI but it should work the same on the pro version:

 

 

import groovy.json.JsonSlurper

def json = """
{
	"totalCount": 1375,
	"results": [
		{
		"title": "florence italy may 12 ryder ...",
		"metadata": {
			"caption": "florence italy may 12 ryder ...",
			"standard.license": "RF",
			"base.thumb_large.width": "100",
			"base.thumb_large.height": "150",
			"base.thumb_small_width": "67" }
		},
		{
		"title": "Title number 2...",
		"metadata": {
			"caption": "florence italy may 12 ryder ...",
			"standard.license": "RF",
			"base.thumb_large.width": "100",
			"base.thumb_large.height": "150",
			"base.thumb_small_width": "67" }
		},
		{
		"title": "Title number 3 ...",
		"metadata": {
			"caption": "florence italy may 12 ryder ...",
			"standard.license": "RF",
			"base.thumb_large.width": "100",
			"base.thumb_large.height": "150",
			"base.thumb_small_width": "67" }
		} ]
}
"""

def slurper = new JsonSlurper()
def parsedJson = slurper.parseText( json )
def resultsCount = parsedJson.results.size

for (int i = 0; i < resultsCount; i++) {
	assert parsedJson.results[i].metadata.caption.equals( "florence italy may 12 ryder ..." )	
}

Cheers! Cat Very Happy



If my answer helped please click on the 'Accept as Solution' button.
New Here?
Join us and watch the welcome video:
Announcements
Top Kudoed Authors