Forum Discussion

donaldngo's avatar
donaldngo
Occasional Contributor
13 years ago
Solved

How do i get JSON values from a response in Groovy

Hi I figured out how to grab NodeValue from XML responses in Groovy using:

def groovyUtils = new com.eviware.soapui.support.GroovyUtils( context )
def holder = groovyUtils.getXmlHolder ("create a TSA user#Response")

//Get account ID from the response
context.accountId = holder.getNodeValue ("//accountId")
log.info ("Storing context.accountId=" + context.accountId );

How would I do the same if the response is in JSON?

Thanks!
  • Here is the later part of the script:

     

    It was not working because, items is a Json Array.

     

    json.items.eachWithIndex{ item, index ->
      log.info "Item #: ${index}"
      log.info "Item resource Id: ${tem.resourceId}"
      log.info "Item name : ${item.name}"
      log.info "Item resource type : ${item.resourceType}"
      log.info "Item status : ${item.status}"
    }

13 Replies

  • This set up worked for me

    ****json

    {
    "odata.metadata": odataCallLocation/@Element",
    "EmployeeNumber": "000000101",
    "nameFirst": "Soap",
    "nameLast": "UI",
    }


    ****groovy script
    import com.eviware.soapui.support.XmlHolder
    import groovy.json.JsonSlurper

    def response = context.expand( '${TestStepName#Response}' ).toString()
    log.info(response)

    def slurper = new JsonSlurper()
    def json = slurper.parseText response

    log.info(json.EmployeeNumber)
    assert json.nameFirst == "Soap"
    assert json.nameLast == "UI"
  • you can get Json String from Response using below Groovy Script

    import com.eviware.soapui.support.XmlHolder
    import net.sf.*;
    import net.sf.json.*;
    import net.sf.json.groovy.*;

    def groovyUtils = new com.eviware.soapui.support.GroovyUtils(context)

    //Getting response from methods
    def ResponseMessage = testRunner.testCase.testSteps["TestStepName"].testRequest.response.contentAsString
    def jsonresponse = new XmlHolder(ResponseMessage)
    def resonseBody = jsonresponse .prettyXml

    now you parse it into Json object and

    GJson.enhanceClasses()
    def responseAsJsonObject = resonseBody as JSONObject

    accountId = responseAsJsonObject['responseAsJsonObject']

    Regards,
    Sadik Ali
  • Guyz,
    I guess this is pretty old but I found a easier way using jsonSlurper-

    JSON Response-
    {
    "IDs": [
    1,
    27
    ],
    "Items": {
    "1": {
    "ItemName": "Item1",
    "ItemType": "Type1",
    "ItemVersion": 5,
    "ItemUrlLocation": "a/b/c/1.zip"
    },
    "27": {
    "ItemName": "Item2",
    "ItemType": "Type2",
    "ItemVersion": 5.3,
    "ItemUrlLocation": "a/b/c/d/e/1.zip"
    }
    },
    "status": {
    "responseStatus": "Success",
    "responseCode": 200
    }
    }

    ##### Groovy Script ########

    import com.eviware.soapui.support.XmlHolder
    import net.sf.*
    import net.sf.json.*
    import net.sf.json.groovy.*

    def ResponseMessage = testRunner.testCase.testSteps["Req_Name"].testRequest.response.contentAsString
    def ItemID, ItemName, ItemType, ItemVersion, i =0
    def jsonSlurper = new JsonSlurper().parseText(ResponseMessage)
    log.info "${jsonSlurper.status.responseCode} ${jsonSlurper.status.responseStatus}"
    while(jsonSlurper.ItemIDs!=null){
    ItemID = jsonSlurper.ItemIDs
    ItemName = jsonSlurper.ItemIDs.toString()]ItemName
    ItemType = jsonSlurper.ItemIDs.toString()]ItemType
    ItemVersion = jsonSlurper.ItemIDs.toString()]ItemVersion
    log.info "$ItemID::$ItemName::$ItemType::$ItemCriticality::$ItemVersion"
    i++
    }
  • skolli143's avatar
    skolli143
    New Contributor
    Hi,

    I am bit new to Groovy Scripting and handling JSON Resonse .

    I have a following JSON Response :
    JSON Response :
    ----------------
    {
    "altmetric_id": 1492155,
    "counts": {
    "readers": {
    "citeulike": "4",
    "connotea": "0"
    },

    "demographics": {
    "poster_types": {
    "member_of_the_public": 19,
    "researcher": 12,
    "practitioner": 6,
    "science_communicator": 2
    },
    "geo": {"twitter": {
    "DK": 2,
    "ES": 3,
    "US": 17,
    "GB": 1,
    "JP": 3,
    "IT": 1,
    "MX": 2,
    "VE": 2
    }}
    },

    "images": {
    "small": "https://altmetric-badges.a.ssl.fastly.net/?size=64&score=26&types=fgtttttt",
    "medium": "https://altmetric-badges.a.ssl.fastly.net/?size=100&score=26&types=fgtttttt",
    "large": "https://altmetric-badges.a.ssl.fastly.net/?size=180&score=26&types=fgtttttt"
    },
    "score": 25.45,
    "last-update-time": "2013-06-10 14:27:09",
    "created": "2013-05-30 12:07:39"
    }

    ---------------------------------

    I need to capture following dates from above response ..
    "last-update-time": "2013-06-10 14:27:09",
    "created": "2013-05-30 12:07:39",
    Then need to some date related comparisons..

    Can anyone let me know how to get "last-update-time" & "created" dates and store in two variables ?

    Regards,
    Suresh
  • skolli143's avatar
    skolli143
    New Contributor
    Hi Sadik Ali,

    What is "Req_Name" ? , Is it Request name or anything else ?

    def ResponseMessage = testRunner.testCase.testSteps["Req_Name"].testRequest.response.contentAsString


    Regards,
    Suresh