ServiceNow Data Stream Action JSON Path Syntax


If you are creating a Flow Designer Data Stream Action, you’ve encountered that you should Split and then Process your results using either an XML or a JSON path. I’ve found that this splitting via JSON Path has some quirks, and does not completely follow the JSON Path specification. In particular, if you have a response in JSON that contains an array, which contains another array, you may have trouble splitting the response down to the second array. Using the Battle.net / World of Warcraft API as an example, a user may have one or more accounts, and then one or more characters within each account. The /profile/user/wow API returns a response like this:

{ 'wow_accounts': [
{ 'characters': [
{ 'name': '1'}, {'name': '2'}
]}
]}

If you follow the JSON Path spec, and want to return all Characters to the data stream action, you may think to use a JSON path like this:

$.wow_accounts[0].characters

This should work since most users only have one account. JSON Path testers online say this works. But then ServiceNow will throw an error like:

Failed to iterate on data stream: com.glide.transform.transformer.exceptions.InvalidPathException: Could not find path in stream: $.wow_accounts[0].characters

You might even try a simpler JSON path like:

$.wow_accounts[0]

And be surprised that it returns an empty array! But if you take the even simpler example:

$.wow_accounts

Then you are returned a record for each account, but it still contains that inaccessible array of characters. So the problem seems to come from the index.

What I found is that it appears that ServiceNow has a defect in the way that it splits dot notation paths. Based on my testing, ServiceNow uses a naive split on the period, with no awareness of indexes. So it splits the first path into 3 identifiers:

1: $
2: wow_accounts[0]
3: characters

And tries to match against the literal path “wow_accounts[0]” which obviously doesn’t exist. So there are two solutions. The first is that you can cheat, and use an invalid JSON Path, but one that works in ServiceNow: “$.wow_accounts.[0].characters”. I do not recommend this.

The better solution is to use the brackets notation when defining your JSON Path. That seems to follow an alternative processing path that does not have the defect. So if you use an identifier like this:

$['wow_accounts'][0]['characters']

Then your Flow Designer Data Stream action will successfully parse your response.


Leave a Reply

Your email address will not be published. Required fields are marked *