convert Reponse string to array of objects

gopijasti
Tera Contributor

hi , I am working with API and I get my response content as shown below .. how do i use script in Action flow designer to convert to Array of objects ?

[
{
"transactionId": 11,
"typeOfRequest": "Add",
"priority": 0,
"employeeNumber": 121
},
{
"transactionId": 12,
"typeOfRequest": "Cancel",
"priority": 0,
"employeeNumber": 122
},
{
"transactionId": 13,
"typeOfRequest": "Cancel",
"priority": 0,
"employeeNumber": 1111
}]

1 ACCEPTED SOLUTION

This should be working based on my testing with your provided JSON example.

 

I think the issue is in your script. You have "outputs.requests = parsedArray," but according to your screenshot, your output variable is named "parsedarray. You script should look like this instead:

(function execute(inputs, outputs) {

    var parsedArray = JSON.parse(inputs.ResponseContentString);
    outputs.parsedarray = parsedArray; // The name of the variable for "outputs.<name>" should match your step outputs exactly.

})(inputs, outputs);

 

It might be useful to use more distinct names for your variables and outputs as that might be causing a little confusion. So instead of parsedarray for your step output, maybe something more like Employee Requests [employee_requests].

 

See attached for examples of what I tested.

View solution in original post

5 REPLIES 5

mlopezrivera
Tera Expert

Hey there! 

May need some more context here, but within a script what you've pasted can be parsed into an array by using: 

JSON.parse(<response string>);

If you're building a data stream action for a Data Source, I'd recommend you take a look at the Dev site as it has a great tutorial for this: Data Stream Actions

Thank you , i tried as suggested above , but I still get null output response. I am attaching screenshots for reference.

 

My goal - I get a string which contains a jsonformatted value. I want to convert this to array of objects so that I can use for loop to iterate each child in array  outside the action in flow designer . 

2.jpg

1.jpg

  

This should be working based on my testing with your provided JSON example.

 

I think the issue is in your script. You have "outputs.requests = parsedArray," but according to your screenshot, your output variable is named "parsedarray. You script should look like this instead:

(function execute(inputs, outputs) {

    var parsedArray = JSON.parse(inputs.ResponseContentString);
    outputs.parsedarray = parsedArray; // The name of the variable for "outputs.<name>" should match your step outputs exactly.

})(inputs, outputs);

 

It might be useful to use more distinct names for your variables and outputs as that might be causing a little confusion. So instead of parsedarray for your step output, maybe something more like Employee Requests [employee_requests].

 

See attached for examples of what I tested.

gopijasti
Tera Contributor

Excellent ! Thank you very much Zach ! appreciate your time and clear explanation ..