Forum Discussion

KeyofSea123's avatar
KeyofSea123
Contributor
13 years ago

JsonSlurper and Overflow

I'm not new to SoapUI but I'm pretty new to Groovy scripting. I'm supporting a web services team. We have Soap requests and responses which are fine. We have REST requests and responses which are fine. We have REST requests with JSON responses and this is where I'm having some difficulty.

This script is for the REST response and it works. I need to create a script for the JSON response that accomplishes the same thing.

***********************************************

import com.eviware.soapui.support.XmlHolder

// Update the 3 lines below to copy paste this code.
def testCase = context.expand( '${DataSource CGD3#TestCase}' )
def testCaseNumber = context.expand( '${DataSource CGD3#TestCaseNumber}' )

def stepName = "Get_Child_Guaranteed_Detail3"
def request = testRunner.testCase.testSteps[stepName].testRequest
def response = testRunner.testCase.testSteps[stepName].getPropertyValue("Response")

def msgExch = request.messageExchange
def rawRequest = new String(msgExch.rawRequestData)
log.info rawRequest

def dateString = new Date()
folderName = context.expand('${projectDir}') + "/Messages/" + dateString.format("YYYY_MM_dd")

def createFolder = new File(folderName)

if (!createFolder.exists())
{
createFolder.mkdirs()
}

def responseHolder = new XmlHolder(response)
def file = new File(folderName+'/'+ stepName + '.txt')
def fw = new FileWriter(file,true)
def pw = new PrintWriter(fw);
pw.println("###################################################################################")
pw.println( "$dateString-----------TestCase: $testCaseNumber $testCase -------------------")
pw.println("###################################################################################")
pw.println(rawRequest)
pw.println("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~")
pw.println(responseHolder.prettyXml)

pw.close()

// Clean up
responseHolder.clear()


***********************************************

I've been trying to use the JsonSlurper but I can't seem to get it to work right. The closest that I've come is an Overflow error. This is what I have so far. The JSON response is 113 lines, if that matters.

***********************************************

import groovy.json.*

// Update the 3 lines below to copy paste this code.
def testCase = context.expand( '${DataSource CGD3#TestCase}' )
def testCaseNumber = context.expand( '${DataSource CGD3#TestCaseNumber}' )

def stepName = "Get_Child_Guaranteed_Detail3_JSON"
def request = testRunner.testCase.testSteps[stepName].testRequest
def response = testRunner.testCase.testSteps[stepName].testRequest.response.contentAsString
log.info response

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


***********************************************

I'm not sure where to go from here. Any help will be extremely appreciated.
  • SmartBear_Suppo's avatar
    SmartBear_Suppo
    SmartBear Alumni (Retired)
    Hi,

    hmmm... is this the error you are getting? http://jira.codehaus.org/browse/GROOVY-4903

    In that case (or even if not), please try downloading the latest groovy release from http://dist.groovy.codehaus.org/distrib ... -1.8.7.zip, unzip it into some local folder and replace the <soapui-pro>\lib\groovy-all-1.8.0.jar file with the groovy-all-1.8.7.jar file in the "embeddable" folder in groovy distribution (thus upgrading to the latest version of groovy).

    Does that help?

    regards!

    /Ole
    SmartBear Software
  • Thanks again! That fixed the overflow error but I'm still having problems with the code. These lines are end of the code above.

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


    I'm getting this message even though the log.info response shows the entire JSON response.

    Wed Aug 08 09:00:34 PDT 2012:INFO:null


    Thanks for all you assistance!