I have an array coming from an api and inside that array is a sub array where it gives a client count for each client site. Each client can have as many of these as are necessary. Think locations. Most clients have 1, some have 2, I have one client with 5. My issue is I need to take the agent_count from each site and add them together. I’ve read the looping doc, I’ve read multiple posts on here and i’m not following what n8n needs to accomplish this. in php i create a variable and then “for each” over the array and over the sub array, doing the math in the second for each to add it all up. I need to store the result into a DB.
So for each client id, i need to loop over these and add together the agent count field getting a total for the client. The example client below should end up with 74.
I’ve tried a split out and that does give me separate objects for those sub arrays, I can’t figure out what i need to use to now add these up based on an id field or client field.
here is the json:
[
{
"id": 28,
"name": "Some Company",
"server_policy": 17,
"workstation_policy": 16,
"alert_template": null,
"block_policy_inheritance": true,
"sites": [
{
"id": 79,
"name": "Anderson SC",
"server_policy": null,
"workstation_policy": null,
"alert_template": null,
"client_name": "Some Company",
"client": 28,
"custom_fields": [],
"agent_count": 23,
"block_policy_inheritance": false,
"maintenance_mode": false,
"failing_checks": {
"error": false,
"warning": true
}
},
{
"id": 78,
"name": "Main",
"server_policy": null,
"workstation_policy": null,
"alert_template": null,
"client_name": "Some Company",
"client": 28,
"custom_fields": [],
"agent_count": 51,
"block_policy_inheritance": false,
"maintenance_mode": false,
"failing_checks": {
"error": true,
"warning": true
}
}
],
"custom_fields": [
{
"id": 38,
"field": 1,
"client": 28,
"value": null
},
{
"id": 39,
"field": 2,
"client": 28,
"value": null
},
{
"id": 46,
"field": 5,
"client": 28,
"value": "97"
}
],
"failing_checks": {
"error": true,
"warning": true
}
}
]
Please share your workflow
Share the output returned by the last node
Information on your n8n setup
- n8n version: 1.91.2
- Database (default: SQLite):SQlite
- n8n EXECUTIONS_PROCESS setting (default: own, main):Default
- Running n8n via (Docker, npm, n8n cloud, desktop app): Docker
- Operating system: Linux