Ram Adabala
Ram Adabala

Reputation: 11

SOAPUI - Parse different JSON Response objects to fetch the same child node values using Groovy Scripting

I'm automating REST API's using SOAPUI. I've 2 different resources and below are the Response JSON formats. The root element varies but the status block remains same for both the response jsons. After the Rest API POST call, I've a Groovy script in place to validate the response (if "code" == "00")

Goal: Using Groovy Script, I've to Parse through the JSON and retreive the "code" node value irrespective of the root element.

Response(JSON) Formats:

{
    "resouurce_1_response":
    {
        "status":
        {
            "code": "00"
        }
    }
}

{
    "resource_2_response":
    {
        "status":
        {
            "code": "00"
        }
    }
}

Upvotes: 0

Views: 415

Answers (1)

tim_yates
tim_yates

Reputation: 171054

One quick way of doing it would be:

def json1 = '{ "resouurce_1_response": { "status": { "code": "00" } } }'
def json2 = '{ "resource_2_response": { "status": { "code": "00" } } }'

import groovy.json.*

def slurper = new JsonSlurper()

assert slurper.parseText(json1).find().value.status.code == '00'
assert slurper.parseText(json2).find().value.status.code == '00'

Of course, if your actual Json is more complex than you show, you might need to do something different (recursively walk the map?)

Upvotes: 1

Related Questions