cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
134
Views
5
Helpful
2
Replies
Highlighted
Beginner

CVP Set_Value Element JSON Parse Array child

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
Beginner

Re: CVP Set_Value Element JSON Parse Array child

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

2 REPLIES 2
Beginner

Re: CVP Set_Value Element JSON Parse Array child

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

Beginner

Re: CVP Set_Value Element JSON Parse Array child

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.  

CreatePlease to create content
Content for Community-Ad
August's Community Spotlight Awards