I have a workflow function making an http request that returns its body as a json string. Can the workflow iterator parse this, or will I have to pass the json string to an automation script to parse and return the data to the workflow in some other format?
For now, you will need to parse it in an automation script. Here are the docs for using automation scripts within workflows. This might improve in one of the upcoming releases.
Does this mean that it is not possible to obtain data in a json using workflow expressions/functions?
For example, if a workflow function making a http request returns json,
{
âusernameâ : âtestuserâ ,
âstatusâ : âokâ
}
If I assign the json to variable Data, can I access the string data in json using Data.username or Data[username] ? Or is there some other way to access the json data within the workflow?
Since the introduction of integration gateway there now is an easier way to work with JSON.
The workflow
The exported JSON is available here.
TL;DR
- Use the
Process arbitrary data in jsenv
function that usesJSON.parse(input)
to parse the given JSON string - Use the iterator of type
item
to iterate over the items (the JSON is represented asVars
).
More explanations
Here is how the entire example workflow looks like:
We firstly use a function step of type process arbitrary data in jsenv
to run arbitrary JavaScript which we can use to parse the JSON string into the real thing.
The output gets represented as Vars
which will allow us to access the properties like any other object (obj.key
for example).
If youâre just parsing a single object you can call it a day right there.
If youâre parsing an array of objects, we probably want to iterate over them.
To do that, we can use an iterator of type items
.
We pass in the parsed JSON and define the output variables.
To log the output Iâll use the function step of type log debug message
.
Do make sure that your log levels are properly set if youâre going to use this step.
When we run it, the output should be as expected:
Thank you for the reply!
I tried the example workflow given and it works on my end, I now understand better how to work with JSON using workflow.
However, when I tried to pass the body from the HTTP request to the Process arbitrary data in jsenv
function in the scope field, I received an error: âcould not exec jsenv function: could not run function: SyntaxError: EOF at parse (native)â
When I manually entered the JSON received into the jsenv function, I could obtain the data, so I am confused what went wrong.
The API I called using the HTTP request is https://reqres.in/api/users which I found online. Is there an issue with my functions?
Can you try using the JavaScript mentioned here? So JSON.parse(readRequestBody(input.Get('request').Body));
@peter can you give us a hand on how HTTP responses should be handled?
Hi @nutella, love the name.
We have been in the process of optimizing the gateway functionality, but in the process somehow managed to break the body reading part.
But, all is not lost.
There are 2 ways to fix this:
- wait for a week for a .8 release
- revert back to 2021.9.4 and use the body that is provided to you via the integration gateway
Should you choose the second option, I have posted a similar answer to an older post, please check here: How to get request body in Workflow API
Cheers, Peter
Thank you both for the help!
When I tried this: JSON.parse(readRequestBody(input.Get(ârequestâ).Body)); , I also encountered an error.
I will try out using the second option to request body as Peter mentioned. Thanks!
Hello again!
I read the post on How to get request body in Workflow API and read up more on the documentation on Integration Gateways and Workflow processing, but I am still confused on how Integration Gateways work along side workflows.
The workflow solution on the older post does not have a HTTP request and immediately logs the payload, does this mean the integration gateway already calls the API for the workflow? I understand the process of the example workflow given by tjerman in this post but I am a little lost on gateways.
I tried using the filters on the integration gateways including just the workflow processor but I cannot seem to obtain a payload or body in my workflow.
Can I get some help on this?
Thank you!
Hi @nutella ,
did you maybe check the link (to the other post) I provided to you?
Currently, the body read as it is described in the docs is broken for another week or so, so your best bet would be to check that link.
In it, you have an example of a workflow that reads from the request body.
The script:
var data
try {
data = JSON.parse(input)
} catch (e) {
throw new Error('could not parse user info payload: ' + e)
}
return data
If you need just the payload in the string format, you can use the payload var that already exists in the workflows.
Note, this is running on 2021.9.4
Note, starting from 2022.3.0-rc.1 (more at Workflow processing :: Corteza Docs), the integration gateway and workflow connectivity has changed somewhat and is encouraged to use over the old versions.