parse the json response with field name containing slash '/'
Hi,
I have to parse the json response and get the value of a field "bookName": "MyBook 001"
Here is my response:
{
"meta": {
"/data/library": {
"bookName": "MyBook 001",
"/book/ID": {
"readOnly": true,
"optional": false,
"hidden": true
} }
}}
This is my script assertion:
import groovy.json.JsonSlurper
def ResponseMessage = messageExchange.response.responseContent
def jsonResponse = new JsonSlurper().parseText(ResponseMessage)
def bName =jsonResponse.meta.data/library.bookName
log.info bName
I get error as no such property library
I tried using Soapui 'get data' option. But context.expand is not working when running from maven/jenkins.
How do i parse this respnse containing data/library?
This is primarily a groovy question. There are actually a couple of ways you can handle this.
The simplest change is just to quote your fields, so change your line below to this:
def bName =jsonResponse.meta."data/library".bookName
It looks weird, but you need quotes because / is a valid character for identifiers in groovy, but not in java.
Here is the documentation for this feature.
http://groovy-lang.org/syntax.html#_quoted_identifiers
The documentation for the JsonSlurper class says that the parseText method returns maps and lists, so you could optionally use the native syntax for those classes like jsonResponse["/data/library"]["bookName"] but then you are using quotes anyway.
Jack