1

I have the following Json response:

 {
    "0P0000G5ZQ":    [
    {
    "Title": "PIMCO Unconstrained Bond Inst",
    "ResourceId": "587723",
    "PublicationTime": "2013-03-07 14:13:00 -0600",
    "Type": "Research",
    "Href": null,
    "VideoThumbnail": null,
    },
    {
    "Title": "Nontraditional Bond 101",
    "ResourceId": "609234",
    "PublicationTime": "2013-08-27 06:00:00 -0500",
    "AuthorName": "Josh Charney",
    "Type": "News"
    "VideoThumbnail": null,
    },
    {
    "Title": "Investors on the Move as Rates Rise",
    "ResourceId": "607677",
    "PublicationTime": "2013-08-16 06:00:00 -0500",
    "AuthorName": "Christine Benz",
    "Type": "Video",
    "SubType": "MSTARVDVD",
    "VideoThumbnail":
    "http://im.mstar.com/im/videocenter/130814_flows_largethumb.jpg",
    }
    ],
    "0P0000PZCB": [],
    "0P00002PYR":    [
    {
    "Title": "FPA New Income",
    "ResourceId": "578826",
    "PublicationTime": "2012-12-26 00:00:00 -0600",
    "AuthorName": "Sarah Bush",
    "Type": "Research",
    "Href": null,
    "VideoThumbnail": null,
    },
    {
    "Title": "FPA New Income, Inc. 2nd Quarter 2013 Conference Call",
    "ResourceId": "BWIPREM_20130719005736",
    "PublicationTime": "2013-07-19 12:32:00 -0500",
    "Source": "Business Wire",
    "Type": "News",
    "SubType": "BWIPREMBWIPREM",
    "VideoThumbnail": null,
    "AuthorThumbnail": null
    }

    ]
    }

I need to print the following nodes from the response ex:"0P0000G5ZQ", "0P0000PZCB", "0P00002PYR" etc and then within each of these nodes I need to assert if the "Title" node is present. The response nodes:"0P0000G5ZQ", "0P0000PZCB", "0P00002PYR" keep on changing depending on the service I am running so I need to always obtain it from the response and not hardcode it. I have to do this in a script assertion in SoapUI.

I have tried to use Json Slurper to get the nodes:"0P0000G5ZQ", "0P0000PZCB", "0P00002PYR" etc as follows:

import com.eviware.soapui.support.XmlHolder
import org.apache.commons.lang.StringUtils
import groovy.json.JsonSlurper 

def holder = new XmlHolder(messageExchange.responseContentAsXml)
def response = messageExchange.response.responseContent
log.info response

def slurper = new JsonSlurper()
def json = slurper.parseText(response)
log.info json.each

but this returns the following INFO:null.

Can anyone provide me a sample code to do this? Thanks.

2
  • don't print json.each just print json Commented Sep 3, 2013 at 20:58
  • Just printing json gives the entire structure. I just want the Id "0P0000G5ZQ".But instead it gives this:INFO:{0P00002CPS=[{Source=PR Newswire, PublicationTime=2013-08-16 11:27:00 -0500, VideoThumbnail=null, ResourceId=PRNews_20130816PH65337, Type=News, AuthorThumbnail=null, SubType=PRNPREMPRNPREM, Title=Aberdeen Global Income Fund, Inc. Announces Payment Of Monthly Distribution} Commented Sep 3, 2013 at 21:03

1 Answer 1

2
def slurp = new groovy.json.JsonSlurper().parseText(jsonStr)
slurp.each{key, val ->
    val.each{
        assert "Title" in it.keySet()
    }
}

where jsonStr represents the above json response in string. (enclosed in '''yourJson''')

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

2 Comments

Thanks for your reply.This works. I just wanted to ask you that would the assertion fail in the following case for the node "0P0000PZCB" as it has no values in it or would it just skip that node and assert for the next one that has values in it? I need to skip the nodes that do not have any values as the "Title" node is optional and I only need to assert if that node has any content : "0P0000PZCB": [], "0P00002PYR": [ {
@user2743905 Yes, it will skip the node which does not have any value. In your case, 0P0000PZCB will not be taken into account at all.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.