Count where two parameters meet a condition

I need to count where two parameters in each item match a certain condition. My JSON looks like this:

[
{
"id": "12"
"LicenseA": "True"
"LicenseB": "False"
}
{
"id": "34"
"LicenseA": "True"
"LicenseB": "True"
}
{
"id": "56"
"LicenseA": "True"
"LicenseB": "False"
}
{
"id": "78"
"LicenseA": "False"
"LicenseB": "False"
}
]

And I want to count each id that where both LicenseA and LicenseB = ‘True’, each where LicenseA = "True’. I’m using IF nodes to handle this, and then merging the data back afterwards, however it breaks if one of the conditions is not met (as that particular branch doesn’t fire, and so data is then missing). I’m sure this must be achievable using code instead of a nest of IF nodes, but I can’t figure it out. Can anyone help?
Side note: I would also want to create a list of which ids meet each condition, but I can probably figure that out once I can get the main count function working.

Please share your workflow

Share the output returned by the last node

Nothing is returned because one of the branches does not fire, however I would expect it to look like:
[

{

“Both_1”: 1,

“License A Only_1”: 2,

“License B Only”: 0,

“Neither”: 1

}

]

Information on your n8n setup

  • n8n version: 1.24.1
  • Database SQLite
  • n8n EXECUTIONS_PROCESS setting own
  • Running n8n via: Docker
  • Operating system Ubuntu

It looks like your topic is missing some important information. Could you provide the following if applicable.

  • n8n version:
  • Database (default: SQLite):
  • n8n EXECUTIONS_PROCESS setting (default: own, main):
  • Running n8n via (Docker, npm, n8n cloud, desktop app):
  • Operating system:

I’ve updated the post with that information now.

Update: we managed to solve this ourselves internally. In case it’s of use to anyone else, the code we used is:

let bothLicenses = items.filter((item) => item.json.LicenseA === 'True' && item.json.LicenseB === 'True')

let licenseAOnly = items.filter((item) => item.json.LicenseA === 'True' && item.json.LicenseB === 'False')

let licenseBOnly = items.filter((item) => item.json.LicenseA === 'False' && item.json.LicenseB === 'True')

return {
  bothLicenses: bothLicenses.length,
  licenseAOnly: licenseAOnly.length,
  licenseBOnly: licenseBOnly.length,
}

And the workflow is now much more simple:

2 Likes

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.