# Access JSON key in Code Node function

**URL:** <https://community.n8n.io/t/access-json-key-in-code-node-function/47313>\
**Category:** Questions\
**Tags:** data-transformation, javascript, code\
**Created:** [May 28, 2024, 4:30pm UTC](https://community.n8n.io/t/access-json-key-in-code-node-function/47313 "2024-05-28T16:30:44Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![d8rt8v](https://dub1.discourse-cdn.com/n8n/user_avatar/community.n8n.io/d8rt8v/32/38541_2.png) [@d8rt8v](https://community.n8n.io/u/d8rt8v)\
**Post date:** [May 28, 2024, 4:30pm UTC](https://community.n8n.io/t/access-json-key-in-code-node-function/47313/1 "2024-05-28T16:30:44Z")

</div>

## 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

---

<div class="post-metadata">

**Author:** ![d8rt8v](https://dub1.discourse-cdn.com/n8n/user_avatar/community.n8n.io/d8rt8v/32/38541_2.png) [@d8rt8v](https://community.n8n.io/u/d8rt8v)\
**Post date:** [May 28, 2024, 9:11pm UTC](https://community.n8n.io/t/access-json-key-in-code-node-function/47313/2 "2024-05-28T21:11:25Z")

</div>

Solved it! This does the trick 😀

```auto
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;

```

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/n8n/user_avatar/community.n8n.io/system/32/195824_2.png) [@system](https://community.n8n.io/u/system)\
**Post date:** [June 4, 2024, 9:12pm UTC](https://community.n8n.io/t/access-json-key-in-code-node-function/47313/3 "2024-06-04T21:12:23Z")

</div>

Closed on June 4, 2024, 9:12pm UTC

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