Blog | Home of Functional and Web Services Automation Testing using Coded UI and SOAPUI/PRO


16

Apr 2014

Reading JSON Request and Response in SOAPUI using Groovy Scripting

This blog talks about how to read JSON request and JSON response in API testing using Groovy in SOAP UI. Let’s take an example of the below JSON request which has 2 attributes (EmployeeID and Department) and the response of this request has 4 attributes (EmployeeID, Name, Designation and Location).

 

JSON Request and Response

 

In order to read JSON in groovy we need to import JsonSlurper which has inbuilt method ParseText which helps in parsing JSON content.

*** Groovy Code Below *** :

import groovy.json. JsonSlurper

def slurper = new JsonSlurper()

 def request = context.expand( ‘${RequestName#Request}’ )

def response = context.expand( ‘${RequestName#Response}’ )

 **** Reading the different attributes of Request ****

def jsonReq = slurper.parseText(request)

log.info jsonReq. EmployeeID

log.info jsonReq. Department

 **** Reading the different attributes of Response ****

def jsonRes = slurper.parseText(response)

log.info jsonRes. EmployeeID

log.info jsonRes. Name

log.info jsonRes. Designation

log.info jsonRes. Location

 

Leave a Reply

Your email address will not be published. Required fields are marked *

Blue Captcha Image Refresh

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>