08-22-2019 10:01 AM
I'm trying to use the native Rest client in studio.
I'm able to get everything from the JSON response except when there is a child element in the json response that is an array type. I need to be able to convert the array to a string value, can I use an iterator within the Set_Value element to return a string
response_body =
{ "someString":"I am a string", "someArray":[1,2,3,4] }
Set_Value -->
importPackage(com.audium.server.cvpUtil); var val= {Data.Element.Rest_Client_01.response_body}; var path = "$.someArray"; JSONPathUtil.eval( val, path);
This only returns a "1", it does not iterate over the field, where the desirable outcome would be for it to return a string value "1,2,3,4"
any ideas how i can achieve this w/o writing a custom element to parse it ?
Solved! Go to Solution.
09-03-2019 01:03 PM
Found Solution
//Get JSON Data from Element
var dataObj = JSON.parse({Data.Element.Rest_Element.response_body});
//Data Validation for Array Field Type
var value = function (arr){
if (arr.length > 1) {
// If array has more than one value evaluate as list and join yelding string csv's
return eval(arr).join()
} else {
// if only one value return single string
return arr.toString()
}
}
//call function, will return a csv string
value(dataObj.arrayField);
09-03-2019 01:03 PM
Found Solution
//Get JSON Data from Element
var dataObj = JSON.parse({Data.Element.Rest_Element.response_body});
//Data Validation for Array Field Type
var value = function (arr){
if (arr.length > 1) {
// If array has more than one value evaluate as list and join yelding string csv's
return eval(arr).join()
} else {
// if only one value return single string
return arr.toString()
}
}
//call function, will return a csv string
value(dataObj.arrayField);
09-04-2019 12:11 PM
Pretty confident the JS interpreter is ES5 or earlier, so constants, arrow functions, essentially all the modern luxuries of JS need not apply. I also do not know how much memory is dedicated to the JS interpreter so use caution and large/complex logic should still be written into custom Java elements.
Discover and save your favorite ideas. Come back to expert answers, step-by-step guides, recent topics, and more.
New here? Get started with these tips. How to use Community New member guide