Code Node for Beginners

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 :wink:

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:

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

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

[
  {
    "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

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

NOTE: if you’re using the JMESPath query (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://jsonpathfinder.com, https://jsoneditoronline.org

Dummy data: dummyjson.com

12 Likes

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

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

2 Likes

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

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 and Jmespath filter in code node not working - #5 by jan

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:

return [$input.all()]

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

1 Like

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

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

1 Like

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?

@thibaultmol remove item / delete key …

See Delete Key from Array

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

1 Like

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!