# Code Node for Beginners

**URL:** <https://community.n8n.io/t/code-node-for-beginners/22031>\
**Category:** Tips & Tricks\
**Tags:** core, data-transformation, code\
**Created:** [January 19, 2023, 12:54pm UTC](https://community.n8n.io/t/code-node-for-beginners/22031 "2023-01-19T12:54:18Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![dickhoning](https://dub1.discourse-cdn.com/n8n/user_avatar/community.n8n.io/dickhoning/32/10415_2.png) [@dickhoning](https://community.n8n.io/u/dickhoning)\
**Post date:** [January 19, 2023, 12:54pm UTC](https://community.n8n.io/t/code-node-for-beginners/22031/1 "2023-01-19T12:54:18Z")

</div>

Whilst figuring out how the Code node works and behaves, I worked out a few samples that might help others getting started with this node. The Code node is extremely powerful, but not always easy to understand (at first). Also, the documentation is not always completely/up-to-date, and the error messages aren’t always very helpful, at least to simple mortals like myself 😉

First, it’s essential that you have a basic understanding of the JSON structure, especially the difference between and Object and an Array.

An example of an Object … 1 item:

```auto
 {
    "color": "red",
    "value": "#f00"
 }

```

And example of an Array … 1 or (generally) more items:

```auto
[
  {
    "color": "red",
    "value": "#f00"
  },
  {
    "color": "green",
    "value": "#0f0"
  },
  {
    "color": "blue",
    "value": "#00f"
  }
]

```

The samples I worked out:

NOTE: the output (display) of on Object is [{}] which suggest it’s an array, but it is actually an object {}

When starting with the Code node, you have to specify the mode in which you want it to operate. You’ve got two choices:

- Run Once for All Items … see examples Get Array from Object & Delete Keys For Loop
- Run Once for Each Item … see example Change Structure via Code Node

> **[Built in methods and variables reference - n8n Documentation](https://docs.n8n.io/code-examples/methods-variables-reference/)**
>
> Built-in methods and variables you can use in expressions and code in n8n.

NOTE: use $input.all()[0].json. to get to the JSON data.

> **[JSON with JMESPath - n8n Documentation](https://docs.n8n.io/code-examples/javascript-functions/jmespath/)**
>
> Documentation for n8n, a workflow automation platform.

NOTE: if you’re using the JMESPath query ([https://jmespath.org](https://jmespath.org)) to remove the “” around the key. So [].{“firstName”: first} in JMESPath needs to be [].{firstName: first} in n8n … both the in the Code and Set node

JSON Tools: [https://jsonlint.com](https://jsonlint.com), [https://jsonpathfinder.com](https://jsonpathfinder.com), [https://jsoneditoronline.org](https://jsoneditoronline.org)

Dummy data: [dummyjson.com](https://dummyjson.com/docs/products)

---

<div class="post-metadata">

**Author:** ![dickhoning](https://dub1.discourse-cdn.com/n8n/user_avatar/community.n8n.io/dickhoning/32/10415_2.png) [@dickhoning](https://community.n8n.io/u/dickhoning)\
**Post date:** [January 20, 2023, 1:46pm UTC](https://community.n8n.io/t/code-node-for-beginners/22031/2 "2023-01-20T13:46:10Z")

</div>

And here’s the node with some more examples of Built-in methods and variables, as well as different syntaxes to get the same result …

 ![Screenshot 2023-01-20 at 14.39.26](https://europe1.discourse-cdn.com/n8n/original/3X/3/5/35ccbd884f7b903155b139f8b052ef20899939ad.png)

As well as an example of how to get the same result via expressions in the Set Node.

 ![Screenshot 2023-01-20 at 14.42.24](https://europe1.discourse-cdn.com/n8n/original/3X/0/d/0d580f5a05261a6d4cec3180fd2aa09ab34afe45.png)

---

<div class="post-metadata">

**Author:** ![dickhoning](https://dub1.discourse-cdn.com/n8n/user_avatar/community.n8n.io/dickhoning/32/10415_2.png) [@dickhoning](https://community.n8n.io/u/dickhoning)\
**Post date:** [January 24, 2023, 7:46am UTC](https://community.n8n.io/t/code-node-for-beginners/22031/3 "2023-01-24T07:46:03Z")

</div>

Just when I thought I got it, I found out that there’s a bit more to it 😉

If you’re interested in what I figured out, please visit the following topic at [Jmespath filter in code node not working - #3 by dickhoning](http://community.n8n.io/t/jmespath-filter-in-code-node-not-working/22103/3) and [Jmespath filter in code node not working - #5 by jan](http://community.n8n.io/t/jmespath-filter-in-code-node-not-working/22103/5)

The take away from this, is that you can actually see the JSON data structure in the background by feeding the data into a Code node with the following code:

```auto
return [$input.all()]

```

This gives (at least) me a better insight in what the structure looks like and how to manipulate it 🙂

---

<div class="post-metadata">

**Author:** ![dickhoning](https://dub1.discourse-cdn.com/n8n/user_avatar/community.n8n.io/dickhoning/32/10415_2.png) [@dickhoning](https://community.n8n.io/u/dickhoning)\
**Post date:** [January 25, 2023, 9:57am UTC](https://community.n8n.io/t/code-node-for-beginners/22031/4 "2023-01-25T09:57:53Z")

</div>

One more ( and most likely last) thing on this topic, and just in case you missed it:

> **[Understanding the data structure - n8n Documentation](https://docs.n8n.io/courses/level-two/chapter-1/)**
>
> Documentation for n8n, a workflow automation platform.

And to whom it may concern; perhaps it’s an idea to add this information, or at least a link to it, at:

> **[Data structure - n8n Documentation](https://docs.n8n.io/data/data-structure/)**
>
> Documentation for n8n, a workflow automation platform.

---

<div class="post-metadata">

**Author:** ![thibaultmol](https://dub1.discourse-cdn.com/n8n/user_avatar/community.n8n.io/thibaultmol/32/1222_2.png) [@thibaultmol](https://community.n8n.io/u/thibaultmol)\
**Post date:** [January 25, 2023, 6:22pm UTC](https://community.n8n.io/t/code-node-for-beginners/22031/5 "2023-01-25T18:22:52Z")

</div>

Could you make an example where you take an array from a json input and then output that array but with a specific item removed from it?

---

<div class="post-metadata">

**Author:** ![dickhoning](https://dub1.discourse-cdn.com/n8n/user_avatar/community.n8n.io/dickhoning/32/10415_2.png) [@dickhoning](https://community.n8n.io/u/dickhoning)\
**Post date:** [January 26, 2023, 8:15am UTC](https://community.n8n.io/t/code-node-for-beginners/22031/6 "2023-01-26T08:15:32Z")

</div>

@thibaultmol remove item / delete key …

See [Delete Key from Array](http://community.n8n.io/t/delete-key-from-array/21878)

---

<div class="post-metadata">

**Author:** ![maxT](https://dub1.discourse-cdn.com/n8n/user_avatar/community.n8n.io/maxt/32/31233_2.png) [@maxT](https://community.n8n.io/u/maxT)\
**Post date:** [April 14, 2023, 7:05am UTC](https://community.n8n.io/t/code-node-for-beginners/22031/7 "2023-04-14T07:05:31Z")

</div>

@dickhoning thank you very much for taking the time to write this up for JS and code node newcomers!! 👏 👏 🙏

---

<div class="post-metadata">

**Author:** ![TheEmanuelSampaio](https://dub1.discourse-cdn.com/n8n/user_avatar/community.n8n.io/theemanuelsampaio/32/36590_2.png) [@TheEmanuelSampaio](https://community.n8n.io/u/TheEmanuelSampaio)\
**Post date:** [March 6, 2024, 5:24pm UTC](https://community.n8n.io/t/code-node-for-beginners/22031/8 "2024-03-06T17:24:40Z")

</div>

This f\* saved me, bro! Thank’s a lot!!!  
I was struggling with the oficial doc, and your exemples were everything i needed!

And other big point was urderstand (GPT Helped me) that we always’ll need to return the values in the structure `return {keyName: valueTreatted}`, just a simples comprehension, but don’t know this was driving me mad with a error “You need to return a vector for all objects” or something like this.

So… That is it. Thank you!
