Accessing JSON fields

Hi,
I’ve implemented an HTTP task and I’m showing the JSON result in a form using an expression field. My problem is that I don’t know how to access the values instead of showing the whole response.

The response is similiar to this:

{
“data”: [

  {
       "id": "admin",
       "name": "Test",
   },
   {
       "id": "superAdmin",
       "name": "superAdmin",
   }

],
“total”: 2
}

To see the respone I wrote ${myVar} where myVar is the name I placed in the field “Response varaible name” in HTTP task properties.
My tests where ${myVar}.total and ${myVar.total} but none worked.

Anyone knows if it’s possible to show only “total” value?

Thanks

Hi,

I think the response from API is stored as TEXT in the database. So you cannot directly access the key as its not JSON when you retrieve it from processVariable but a string. You can use something like

import org.flowable.engine.impl.util.json.JSONObject;
JSONObject response = new JSONObject(“String version of your processVariable”);
then do
response.getInt(“total”) to get the value of the total key.

You can expore all other methods to access data from response in JSONObject class.

Hope this helps.

Thank You,
Arpit

Sorry in which file should we include these lines? Thanks!

Hi Pau,

I’m accessing data from http request in variable resp (name is defined in http task) for example in javascript script task.

var resp = execution.getVariable(‘resp’);
var response = JSON.parse(resp);
execution.setVariable(‘resp_total’, response.total);