Convert a REST response to Array of objects

terwilld
Tera Contributor

Hello!

 

This is a Flow action question.  I'm having trouble mapping the response from an API to an array of objects.

I have a rest endpoint that is correctly being polled 

I have a response like:

 

 

[{"_id":"66b196693062280c469621e0","dbusername":"David","sql":"TRUNCATE TABLE Cars;","object":"Cars","verb":"Truncant","serverIP":"123.123.123.111","clientIP":"321.321.321.321","timestamp":"2024-08-04T03:13:20.010Z","timeLogged":"2024-08-06T03:20:09.874Z","__v":0},{"_id":"66b1966c3062280c469621e3","dbusername":"David","sql":"TRUNCATE TABLE Cars;","object":"Cars","verb":"Truncant","serverIP":"123.123.123.111","clientIP":"321.321.321.321","timestamp":"2024-08-04T03:13:20.010Z","timeLogged":"2024-08-06T03:20:12.882Z","__v":0}]

 

 

This is correctly being pulled by a rest Step in a flow action.

What I'm trying to do is correctly use this response to create an array of objects.  I can't even seem to get hard coded responses to appear in the outputs section correct, it does however log correctly.

 

First I'm trying to understand why this doesn't work [this is a simple exercise that will be extended for the above]. 

In a custom script I create a new object, set a key to a value of this new object and set it to outputs.  This variable is then empty when I run the script -> Where does the data go?  It is logged correctly by gs.info()

 

 

var o = new Object();
o.test = 'foo'
outputs.o = o
outputs.o.test = o.test
gs.info("this is my o: "  + o.test)
gs.info("outputs object test" + outputs.o)

 

At the end of the script I then set the "o" variable to be an object

terwilld_0-1723058389055.png

Then at the end of the action, in the outputs for the action  I use this "o" to define a new variable "o2" which references the above:

terwilld_1-1723058478213.png

 

This variable "o2" is then empty when I test the execution of the flow.  I would have expected a key of test and value of foo:

terwilld_2-1723058550305.png


This is the simplest action I expect to work which doesnt, what might be the problem?
The logging for this works: 

terwilld_3-1723058617562.png


What I'm reallly trying to do is build an array of objects from the first chunk - the http response - however I can't even get a hard coded single value of an object to be sent to the outputs of the action stage.

 

 

1 REPLY 1

DMaldonado
ServiceNow Employee
ServiceNow Employee

Did you try something like this

const restValue = inputs.restObjArray

const record1Username = restValue[0].dbusername

gs.info (record1Username)