Forum Discussion

KSN's avatar
KSN
New Contributor
7 years ago
Solved

How to get the property from Json response using Groovy in soapui

Hi ,

 

Below is the json response, from this i need to get the uri based on the name, here name=abc.

Using json.url[0].services[0].config.www[0].uri, i got the desired value but heard that its not the proper way to handle.

Please help me how to get this through groovy script. Thanks in advance.

 

{
  "url": [
    {
      "name": "test",
      "services": [
        {
          "name": "abc",
          "config": {
            "www": [
              {
                "scheme": "https",
                "uri": "https://abc.com"
              }
            ]
          }
        },
        {
          "name": "def",
          "config": {
            "www": [
               {
                "scheme": "https",
                "uri": "https://def.com"
              }
            ]
          }
        }
      ]
    },
    {
      "name": "test1",
      "services": [
        {
          "name": "abc",
          "config": {
            "www": [
              {
                "scheme": "https",
                "uri": "https://abc.com"
              }
            ]
          }
        },
        {
          "name": "def",
          "config": {
            "www": [
               {
                "scheme": "https",
                "uri": "https://def.com"
              }
            ]
          }
        }
      ]
    }
  ]
}

Regards,

Raj

  • I would do something like this...

     

    new groovy.json.JsonSlurper().parseText(json)
     .url.find { it.name == 'test' }
     .services.find { it.name == 'abc' }
     .config.www.uri 

4 Replies

  • JHunt's avatar
    JHunt
    Community Hero

    I would do something like this...

     

    new groovy.json.JsonSlurper().parseText(json)
     .url.find { it.name == 'test' }
     .services.find { it.name == 'abc' }
     .config.www.uri 
    • KSN's avatar
      KSN
      New Contributor

      Thanks JHunt, it worked.

      • nmrao's avatar
        nmrao
        Champion Level 3
        KSN,
        It is appreciated to "Accept as Solution" when the solution works. For now I marked it.