jsonGet vs jsonDecode

I am trying to convert some scripting transforms into expressions, and I've bumped into the fact that the jsonGet expression seems to leave complex objects as strings. For example, this expression returns the string [1,3,5]:

jsonGet("{'aaa':[1,3,5],'bbb':7}","aaa")

But this script returns an array-like object:

return system.util.jsonDecode("{'aaa':[1,3,5],'bbb':7}")['aaa']

Am I missing something here, or is jsonGet lazy? Any time I try to make it return something more complex than just a single int/string/float/bool it just returns a JSON substring.

And while I've got eyes here, is the syntax for the jsonGet path based on any public syntax I can look at? It's simple enough that I can fake it, but I was hoping it had fancier features like JSONPath to aggregate sub-fields.

It's not supposed to return encoded/complex objects, it extracts JSON from JSON. The expression language in general also can't operate on arrays or complex objects.

edit: mmm, I think that's how it's supposed to work with arrays, anyway.

edit2: yes, seems so, but it's a little more nuanced because it actually depends on the type you pass in.

This is the relevant part of the unit tests if it helps you imagine:

if (json instanceof String) {
    assertEquals("[2,1,0]", jsonGet(json, "widget_array[1].d"));
} else if (json instanceof DocumentElement) {
    assertEquals(parseDocument("[2,1,0]"), jsonGet(json, "widget_array[1].d"));
} else if (json instanceof JsonElement) {
    assertEquals(parseDocument("[2,1,0]"), jsonGet(json, "widget_array[1].d"));
}
1 Like

So, to summarize my rambling:

If you feed the function a String that happens to be JSON you get a String that happens to be JSON back.

If you feed it a DocumentElement (i.e. the value from a Document tag) you get a DocumentElement back.

1 Like

You might like the jsonDecode() expression function I recently added to my Integration Toolkit. Returns the complex object you expect. (Not yet documented, as there's more, related, coming.)

1 Like