Access JSON key in Code Node function

Describe the problem/error/question

Hello! I made a JS code to convert my JSON: In case the URL in the elements is the same, i want to make an array from employee key for each match.

I have no problem when declaring my json by pasting it, but i can not understand how to do it with using output from other node, e.g $(‘Code’).all()

I need an advice how to deal with .json key in the n8n code block. There is an example of both outcomes when my code works (in case of manual declaring) and fails to work.

Thank you!

What is the error message (if any)?

No errors, just dont execute the code (retuns initial JSON)

Please share your workflow

Share the output returned by the last node

Information on your n8n setup

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

Solved it! This does the trick :grinning:

const items = $input.all(); 
const urlMap = {};

items.forEach(item => {
    const data = item.json; 

    if (data.URL) {
        if (!urlMap[data.URL]) {
            urlMap[data.URL] = { ...data, concat: [data.employee] };
        } else {
            urlMap[data.URL].concat.push(data.employee);
        }
    } else {
        urlMap[JSON.stringify(data)] = data;
    }
});

const result = Object.values(urlMap).map(item => ({ json: item }));

return result;


1 Like

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