From below response body I'm trying to capture "type" from phoneNumbers array and this is the syntax I'm using $.phoneNumbers[1].type. It works fine as long as that array exists. However my REST response varies every time based on input and array size also changes from 1 to 4.
So question in place is how do I get array count so I don't have to parse JSON objects that doesn't exist.
As of now since max array count is 4, I'm parsing all 4 arrays of "type" and "number" in action element and getting error.
Response Body:
{
"firstName": "John",
"lastName" : "doe",
"age" : 26,
"address" : {
"streetAddress": "naist street",
"city" : "Nara",
"postalCode" : "630-0192"
},
"phoneNumbers": [
{
"type" : "iPhone",
"number": "0123-4567-8888"
},
{
"type" : "home",
"number": "0123-4567-8910"
}
]
}
Error from Activity logs:
05/20/2020 15:44:04.373,jsParseJSON,custom,type,sun.org.mozilla.javascript.internal.WrappedException: Wrapped com.jayway.jsonpath.PathNotFoundException: Array index [Ljava.lang.String;@379bb10a not found in path (<Unknown source>#4) in <Unknown source> at line number 4
05/20/2020 15:44:04.373,jsParseJSON,exit,
05/20/2020 15:44:04.373,,element,error,
Syntax in Set Value element:
importPackage(com.audium.server.cvpUtil);
var inputJSON= {Data.Element.Rest_Client_01.response_body};
var path="$.type[2].cptCodeDescription";
JSONPathUtil.eval(inputJSON,path);