jsonPath for Orchestrator

bg-vcologo

vRealize Orchestrator already ships with a lot of plug-ins for orchestrating all kind of external services. If there is no plug-in available, the preferred way is to connect to a REST web service.

This is all very nice and convenient, but in doing so, we encounter the same issues everyone else does with REST. For example, parsing through all the response data we receive from the web service. Nowadays, this data is mostly typed in json. Json is very clean and readable, however working with json data can sometimes be very exhausting – especially as Orchestrator only comes with limited support for parsing JSON.
Luckily there is a technology called JSONPath, which works in a very similar way to XPath. JSONPath helps to explicitly express the value we are interested in, from a JSON document.

Now we have implmented a plugin that encapsulates the jsonPath API for use in Orchestrator (we use the most common java implementation of jsonPath: https://github.com/jayway/JsonPath).

Our first version of the plug-in provides the „read“-method of the API. Let’s have a look at some examples:

Let’s imagine that a REST web service responded with the following json string:

{
    „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
        }
    }
}

With our plugin we now simply can get some values out of this text:

var result = JsonPathReader.read(jsonString , ‚$.store.book[:1].title‘);

would result in „Sayings of the Century“, because it is the title of the first book of the store.

‚$..book[1,2].title‘ would lead to [„Sword of Honour“,“Moby Dick“]

‚$.store..title‘ and  ‚$..book[*].title‘ to [„Sayings of the Century“,“Sword of Honour“,“Moby Dick“,“The Lord of the Rings“]

And

‚$..book[:3]title‘ to [„Sayings of the Century“,“Sword of Honour“,“Moby Dick“]

You also can get the last book:

‚$..book[-1:].title‘ is „The Lord of the Rings“

Or

‚$..book[-2:].title‘ is [„Moby Dick“,“The Lord of the Rings“]

Then there are conditions:

‚$.store.book[?(@.isbn)]‘ is all books with a isbn:

[
            { „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
            }
        ]

Or

‚$.store.book[?(@.price<8.99)].‘ Is the books with a price lower than 8.99:

                       { „category“: „reference“,
              „author“: „Nigel Rees“,
              „title“: „Sayings of the Century“,
              „price“: 8.95
            }

Or

‚$..book[?(@.category==“fiction“)].author‘ would be [„Evelyn Waugh“,“Herman Melville“,“J. R. R. Tolkien“]

To install the plugin, you always need to login to the vRO control center, then go to „Manage plugins“ and upload the plugin file which you can find HERE.

c-1

After restarting your vRO service, it should look like this:

c2

And in the vRO client you can see the jsonPath command in the API explorer:

c-3

Have fun!

Kommentar absenden

Deine E-Mail-Adresse wird nicht veröffentlicht. Erforderliche Felder sind mit * markiert