Examples: JMS Payload 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 Input box.

{ "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 Input box.

<?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')

XML

If the input can be parsed as XML, an XmlSlurper is used. If the input can be parsed as JSON, then a JsonSlurper is used.

The Slurpers use a '.' notation similar to JSONPath. Below is a sample XML and some examples.

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

<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"

  <soap:Body>

    <StockQuotes>

      <Stock>

    <Symbol>AAPL</Symbol>

    <Last>474.75</Last>

    <Change>+7.39</Change>

    <Open>471.00</Open>

    <Volume>8767926</Volume>

    <Name>Apple Inc.</Name>

      </Stock>

    </StockQuotes>

  </soap:Body>

</soap:Envelope>

To get the Symbol using the XmlSlurper provided in the path variable:

path.Body.StockQuotes.Stock.Symbol

If there were more than 1 Stock then:

path.Body.StockQuotes.Stock[0].Symbol

Note that path represents the top level node (Envelope) and that namespaces are not needed, Body is used for the soap:Body node.

JSON

A JSON example for the JsonSlurper from the previous examples would be:

path.store.book[0].title