Forum Discussion

gkpandi's avatar
gkpandi
Occasional Contributor
12 years ago

how can get the response from JSON format using script?

Hi,

I'm using SoapUI Pro 4.5.0 and also a beginner for this tool. I need to get the value from the response (JSON format). Can anyone give me sample code for this?

Note: I'm using the REST based service not SOAP based service.

3 Replies

  • 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

    Regards,
    Sadik Ali
  • EswarJ's avatar
    EswarJ
    New Contributor
    Hi..
    You can also get the values using Xpath in a property transfer step.
    Though the response is in JSON format, SoapUI allows you to view the response in XML format as well.
    Click on the XML tab in the response window to view this in XML.

    In the property transfer step, choose the appropriate test step as source and in the drop down property choose responseAsXml.

    -Eswar
  • Sadik Ali,

    I'm trying to use the technique that you gave. It's mostly working but I'm getting a CDATA error on the XmlHolder line. I'm not new to SoapUI but I'm pretty new to Groovy. I have some programming experience but this is a new language to me.

    Can you please offer some advice?

    Thanks,
    Trish