user7242550
user7242550

Reputation: 261

Trying to extract the following value from the REST response(SOAPUI)

I have the following SOAPUI rest output:

<html>
   <head>
      <meta content="HTML Tidy for Java (vers. 26 Sep 2004), see www.w3.org" name="generator"/>
      <title/>
   </head>
   <body>
      {"message":{},"records":[{"Application Name_2":"DO NOT DELETE: QA Regression Test","Status ID_5":"13160","Email_6":"","ListFiles Webservices_4":"
      <a download="download" href="/" target="_blank">Test.txt&lt;\/a>,</a>
      <a download="download" href="/" target="_blank">TestTwo.txt&lt;\/a>","# Index_1":"1","DownloadFile Webservices_3":"</a>
      <a download="download" href="/" target="_blank">Test.txt&lt;\/a>"}],"header":[{"index":"1","name":"# Index","numformat":"","type":"string"},{"index":"2","name":"Application Name","numformat":"","type":"string"},{"index":"3","name":"DownloadFile Webservices","numformat":"","type":"string"},{"index":"4","name":"ListFiles Webservices","numformat":"","type":"string"},{"index":"5","name":"Status ID","numformat":"","type":"string"},{"index":"6","name":"Email","numformat":"","type":"string"}]}</a>
   </body>
</html>

I am trying to extract Test.txt but unable to do so.

This is what I have tried:

//imports
import groovy.json.JsonSlurper

//grab the response
def ResponseMessage = messageExchange.response.responseContent
//define a JsonSlurper
def jsonSlurper = new JsonSlurper().parseText(ResponseMessage)

def FieldDownloadFile = jsonSlurper.records.'DownloadFile Webservices_3'
log.info FieldDownloadFile

and this is what I am able to extract:

[<a target=_blank href="/files/spool/493500/1133476/1_2866521_1133476_Test.txt?fs=1" download>Test.txt</a>]

However I only wanna extract - Test.txt

Upvotes: 0

Views: 423

Answers (1)

albciff
albciff

Reputation: 18507

The value you want to parse is an arbitrary string value from one of your Json elements so there is no standard way to do so directly with the slurper.

In your case for example since your string value is an Xml you can parse it again with XmlSlurper to get the desired value. Below is a simplified sample for your case:

import groovy.json.JsonSlurper

def message = '''
 {"message":{},"records":[
    {"DownloadFile Webservices_3":"<a download='download' href='/' target='_blank'>Text.txt</a>"}]}

'''

def jsonSlurper = new JsonSlurper().parseText(message)
def fieldDownloadFile = jsonSlurper.records.'DownloadFile Webservices_3'

// since <a> is the root of your xml you can access the value without
// any path, if your xml was more complex you need a path like JsonSlurper
def anchorValue =  new XmlSlurper().parseText(fieldDownloadFile)

log.info anchorValue // prints Text.txt

Upvotes: 1

Related Questions