Convert a REST response to Array of objects
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-07-2024 12:24 PM
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
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:
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:
This is the simplest action I expect to work which doesnt, what might be the problem?
The logging for this works:
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-10-2024 06:07 PM
Did you try something like this
const restValue = inputs.restObjArray
const record1Username = restValue[0].dbusername
gs.info (record1Username)