Count occurrences of each item in JSON

Excellent - Always good to see how people’s Brain is proceeding to solve a problem.
Thanks for your support & ressources as usual !

3 Likes

This is amazing ! On top on that I’m using AskJArvis.io to help me understand the syntax.

3 Likes

This is fabulous, bookmarking!!

1 Like

Hey @djangelic the ‘length’ option is not appearing in the desktop version of n8n. Do you have any other workaround?

Very strange, what version of the desktop version are you using?

How do we achieve this with the new Code node since you have to reference $input instead of items?

Hi @jhambach, assuming you are on the latest version of n8n the Code should work with the exact same snippets:

If you want to use the new syntax, the code on the Count audience & status node could look like so:

const unique_audiences = [...new Set($input.all().map(e => e.json.Audience))];
const unique_statuses = [...new Set($input.all().map(e => e.json.Status))];

let results = [];

unique_audiences.forEach(audience => {
  let result = {
    json: {
      audience: audience
    }
  };
  unique_statuses.forEach(status => {
    result.json[status] = $input.all().filter(e => e.json.Audience == audience && e.json.Status == status).length;
  })
  results.push(result);
});

return results;

Or as a workflow:

1 Like

Thank you so much for sharing this elegant and concise piece of code ! It would be fantastic if this is embedded in a node ( group by sum, count kind of SQL grouping for JSON objects) as it would make it easy for functional users like myself to adapt using drap and drop. In all cases, thank you again, I am taking this code for now !

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