Forum Discussion

richie's avatar
Community Hero
6 years ago

Grabbing .json response attribute value in output log & passing attribute values to propertiesstep



Ok - so rao put a groovy script together for me that builds a GET request (template and query parameters as required sourcing the names and values from Properties because ReadyAPI! doesn't allow the user to parameterize a REST request's QueryParamater name values).


The script generates the response in the output log and I'm trying to update the script to actually grab an attribute value from the .json response and save as a property


The script is as follows:

import groovy.json.*

//specify the endpoint
def serviceHost = context.expand ('${#Project#TestEndpoint}')
//Below to handle template parameter, additional $ required before
def getNamespaceDatasetPath = '''/api/1/${namespace}/${dataset}'''
//define all template param values as shown below
def binding = [namespace : context.expand( '${#TestCase#namespace}') ,  dataset : context.expand('${#TestCase#dataset}')]
//def binding = [namespace : 'certification-nomenclature', dataset : 'certification']

def template = new groovy.text.SimpleTemplateEngine().createTemplate(getNamespaceDatasetPath)

def queryParams = [:]
//Get the properties of Property Test step to build the query parameters as required
context.testCase.testSteps["Properties"].properties.each {
	queryParams[it.key] = it.value.value
def client = new RESTClient(serviceHost)
def response = client.get(path: template.make(binding) as String,
					 accept: ContentType.JSON,
					 query : queryParams
assert response.statusCode == 200 groovy.json.JsonOutput.prettyPrint(response.text)

//grab the response
def object = new JsonSlurper().parseText(response.text)

//some relevant assertions assert object.header.success == true assert object.header.success == true assert object.meta.type == "certification-nomenclature"
//this is my attempt to alter the script so that it grabs the 'custodian' attribute value and writes it to the Properties2 step
//the JSONPATH to the attribute is as follows (JSONPATH --> $['meta'][custodian])
{ def custodianvalue = (object.meta.custodian) //I'm guessing this bit is the problem! def propertiesStep = context.testCase.testSteps["Properties2"]; propertiesStep.setPropertyValue("Custodian", custodianvalue); }

Now I'm pretty sure I've got everything correct except the 'def custodianvalue = ' line.


I have attached an example of a response


Can anyone point out what I'm doing wrong?


As always - thanks to all!



  • richie ,


    Is this what you would like to do?


3 Replies

  • New2API's avatar
    Frequent Contributor

    richie if custodian is subnode under meta then your code is correct.


    If meta and custodians are separate nodes then all you have to do is - 

     def custodianval = object.custodian


    One more thing to consider if those nodes are an array i.e., nodes are in [ ].

    If so then you need to reffer that using a counter.

    for ex:

    object = {meta: [], custodian: [{name:test1, id:1}, {name:test2, id:2}] }


    def cutodianval = object.cutodian[0] .name  would give you test1


    hope this helps.

    • richie's avatar
      Community Hero

      Hey New2API & @Rao


      thanks and Kudos to both - my script was generating multiple compilation errors on the code snippet to extract the property value:


      org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed: Script15.groovy: 41: Ambiguous expression could be a parameterless closure expression, an isolated open code block, or it may continue a previous statement; solution: Add an explicit parameter list, e.g. {it -> ...}, or force it to be treated as an open block by giving it a label, e.g. L

      @Rao - yep that works - thank you!  Exactly what I need!