cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
1724
Views
10
Helpful
2
Replies

CVP Set_Value Element JSON Parse Array child

Mark Pareja
Beginner
Beginner

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 ? 

1 ACCEPTED SOLUTION

Accepted Solutions

Mark Pareja
Beginner
Beginner

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);

View solution in original post

2 REPLIES 2

Mark Pareja
Beginner
Beginner

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);

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.  

Getting Started

Find answers to your questions by entering keywords or phrases in the Search bar above. New here? Use these resources to familiarize yourself with the community: