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 response area.

{ "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 box as shown below.

Parsing example

XPath

Select XPath in the Parsing Type field and copy the following text and paste it into a parsing's response area.

<?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 box as shown below.

Parsing example

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 box as shown below.

Parsing example

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

XQuery/JSON to XML and XQuery

There is no output conversion when the parsing type is set to XQuery/JSON to XML and XQuery.

Example 1: JSON Array with an Array Name

In this case, JSON is converted to XML and parsed with XQuery.

If JSON Response is JSON Array, like the following example, it cannot be converted to XML, because in this case there is no root node specified.

{"properties":[{"stringProperty":"prop1","intProperty":"1"},{"stringProperty":"prop2","intProperty":"2"},{"stringProperty":"prop3","intProperty":"3"}]}

This is the root node in the generated XML.

<?xml version="1.0" encoding="UTF-8"?>

<uc4TopParent>

   <properties>

      <intProperty>1</intProperty>

      <stringProperty>prop1</stringProperty>

   </properties>

   <properties>

      <intProperty>2</intProperty>

      <stringProperty>prop2</stringProperty>

   </properties>

   <properties>

      <intProperty>3</intProperty>

      <stringProperty>prop3</stringProperty>

   </properties>

</uc4TopParent>

In this case, XQuery should include the root node uc4TopParent in its expression.

For example XQuery:

for $x in $input/uc4TopParent/properties

return $x/stringProperty/text()

The parsing results will be displayed in the Result box as shown below.

Parsing example

Example 2: JSON Array with No Array Name

In this case, the JSON Response is JSON Array with no array name, like the following:

[{"stringProperty":"prop1","intProperty":"1"},{"stringProperty":"prop2","intProperty":"2"},{"stringProperty":"prop3","intProperty":"3"}]

It cannot be converted to XML. In this case array name and root node are generated and XML looks like this:

<?xml version="1.0" encoding="UTF-8"?>

<uc4TopParent>

   <uc4ArrayParent>

      <intProperty>1</intProperty>

      <stringProperty>prop1</stringProperty>

   </uc4ArrayParent>

   <uc4ArrayParent>

      <intProperty>2</intProperty>

      <stringProperty>prop2</stringProperty>

   </uc4ArrayParent>

   <uc4ArrayParent>

      <intProperty>3</intProperty>

      <stringProperty>prop3</stringProperty>

   </uc4ArrayParent>

</uc4TopParent>

As with the axample above, XQuery should include root node and array name when parsing. For example, XQuery:

for $x in $input/uc4TopParent/uc4ArrayParent

return $x/stringProperty/text()

The parsing results will be displayed in the Result box as shown below.

Parsing example