Examples: Web Service Response Parsing
Below are examples for different types of parsings for you to try out. The returned result is a String, it may be a single word, a number, or multiple words. In any case, the whole result is used. Therefore, you might want to first define a parsing that returns multiple results, and then fine tune it to only return a single result. For regular expression parsing examples, see a regular expression document.
JSONPath
Select JSONPath in the Parsing Type field and copy the following text and paste it into a parsing's
{ "store": {
"book": [
{ "category": "reference",
"author": "Nigel Rees",
"title": "Sayings of the Century",
"price": 8.95
},
{ "category": "fiction",
"author": "Evelyn Waugh",
"title": "Sword of Honour",
"price": 12.99
},
{ "category": "fiction",
"author": "Herman Melville",
"title": "Moby Dick",
"isbn": "0-553-21311-3",
"price": 8.99
},
{ "category": "fiction",
"author": "J. R. R. Tolkien",
"title": "The Lord of the Rings",
"isbn": "0-395-19395-8",
"price": 22.99
}
],
"bicycle": {
"color": "red",
"price": 19.95
}
}
}
Put each of the following expressions in the Expressions box and click Test:
- store.book[0]
- store.book[0].title
- store.bicycle
- store.bicycle.color
The parsing results will be displayed in the Result
XPath
Select XPath in the Parsing Type field and copy the following text and paste it into a parsing's
<?xml version="1.0" encoding="iso-8859-15" ?>
<store>
<book>
<author>Nigel Rees</author>
<title>Sayings of the Century</title>
<category>reference</category>
<price>8.95</price>
</book>
<book>
<author>Evelyn Waugh</author>
<title>Sword of Honour</title>
<category>fiction</category>
<price>12.99</price>
</book>
<book>
<author>Herman Melville</author>
<title>Moby Dick</title>
<category>fiction</category>
<price>8.99</price>
<isbn>0-553-21311-3</isbn>
</book>
<book>
<author>J. R. R. Tolkien</author>
<title>The Lord of the Rings</title>
<category>fiction</category>
<price>22.99</price>
<isbn>0-395-19395-8</isbn>
</book>
<bicycle>
<price>19.95</price>
<color>red</color>
</bicycle>
</store>
Put each of the following expressions in the Expressions box and click Test:
- /store/book
- /store/bicycle
- /store/book[1]/title
The parsing results will be displayed in the Result
XQuery/JSON XQuery
Query examples using the same JSON/xml as the JSONPath/XPath examples:
Example 1
for $x in $input/store/book
return $x
Example 2
for $x in $input/store/book[1]
return $x/title
Example 3
for $x in $input/store/book[1]
return $x/title/text()
The parsing results will be displayed in the Result
Groovy
Groovy scripts are passed three variables:
- input: The response text
- path: The JsonSlurper.parseText() or XmlSlurper.parseText() result
- logger: used to write a message to the Job report
Path example:
def title = path.store.book[0].title
def author = path.store.book[0].author
Input example:
int n = input.indexOf ('bicycle')
return input.substring (n)
logger examples:
Example 1
String msg
logger.logMsg (msg)
Example 2
logger.logMsg ('test message')
JSON
A JSON example for the JsonSlurper from the previous examples would be:
path.store.book[0].title