Dynamic access to array of objects to access data

Hello Camunda Community,
I have the following case of an array of objects that I am obtaining by API from a service:

{
“total_items”: 10,
“items”: [
{
“platform”: “other”,
“answers”: [
{
“text”:“carlos”
},
{
email":"carlos@gmail.com
},
{
“phone_number”:“1999999999”
}
]
}
]
}

For “total_items” I am getting the value correctly with:

connector.setVariable(“Total Items”, S(response).prop(“total_items”).value());

How do I access the data in the object array?:
“text”: “carlos”
email":"carlos@gmail.com
“phone_number”:“1999999999”

Initially I am trying the following:

var name = S(response).prop(“items”).elements().get(1).value();
connector.setVariable(“Name”, name);

Getting the following error message: org.camunda.spin.json.SpinJsonDataFormatException: SPIN/JACKSON-JSON-01002 Expected ‘String/Number/Boolean/Null’, got ‘OBJECT’

I haven’t worked with SPIN yet, but looking that the docs and at the error, it looks like your call isn’t getting what you think it’s getting.

var name = S(response).prop(“items”)
should now be getting 1 element:

[
{
“platform”: “other”,
“answers”: [
{
“text”:“carlos”
},
{
“[email":"carlos@gmail.com](mailto:email%22:%22carlos@gmail.com)”
},
{
“phone_number”:“1999999999”
}
]
}
]

Then you’re calling elements() on this, which should break this down into a list to work from… So far so good.

.get(1) gets you the second (it’s a 0-based array - see Fetch Array of Data) item:

“answers”: [
{
“text”:“carlos”
},
{
“[email":"carlos@gmail.com](mailto:email%22:%22carlos@gmail.com)”
},
{
“phone_number”:“1999999999”
}
]

Since this is an array (or object…) when you call .value() on it, it is expected to complain (Value Types )

So… What do you get if your try

var name = S(response).prop(“items”).elements().get(0).value();
connector.setVariable(“Name”, name);
1 Like