n8n expression workflow tutorial

Welcome! This tutorial will guide you through mastering the core and most powerful feature in n8n – expressions, from scratch, using a real-life workflow case. We will break down every step of data processing in a “product inventory management” scenario.

Firstly, if you find it troublesome to build your own system, you can easily use the official solution .Just Click here

How to use this tutorial

To achieve optimal learning outcomes, please study this tutorial alongside the n8n workflow.

  • Import workflow: First, import the n8n learning expression workflow.json file you provided into your n8n canvas.
  • Run the workflow: Click the “Execute” button next to the “Start Learning” node in the top left corner to allow the data to flow through the entire workflow. You will see data outputs appear below each node.
  • Comparative learning: Next, you can read the course explanations below one by one. After completing each lesson, go to the corresponding node (such as “1. Extract Basic Information”) on the n8n canvas, click it to view its settings (input expression) and output (execution result), and witness firsthand how the expression works.
Workflow download, extraction code: 2eJF

Data center: “Product information source” node

All our operations will revolve around the data of this node. It simulates a product information table, with the content as follows:

{

“productName”: “Smart Noise Cancelling Headphones”, “productID”: “HP-2025X”,

“price”: 1299,

“inStock”: true,

Warehouses “: [” Shanghai warehouse “,” Shenzhen warehouse “,” Beijing warehouse “],” supplerInfo “:{

“name”: “Shenghai Electronics”,

“contactPerson”: “Manager Wang”, “phone”: “13800138000”

},

“specs”: [

{ “specName”: “Bluetooth version”, “value”: “5.3” },

{ “specName”: “Endurance time”, “value”: “24 hours” }

]

}

Lesson 1: Obtaining a single piece of data

This is the most basic operation, just like reading data from a cell in an Excel spreadsheet.

Corresponding node: 1. Extract basic information. Objective: Obtain the product name.

Expression: {{ $(productInformationSource).item.json.productName }}

Explanation:

{{ … }} is a marker for expressions.

$(product_info_source) is used to specify “I want to retrieve data from the ‘product_info_source’ node”.

“.item.json” is a fixed writing format, representing “the data item currently flowing through the node”.

“.productName” is the specific field name we want to obtain.

Lesson 2: Locating Data More Precisely

When a node may output multiple data items, we need a more precise “selector” to avoid confusion.

Corresponding node: 2. Precise positioning data

Objective: To obtain the product ID in a more secure manner.

Expression: {{ $(productInfoSource).last().json.productID }}

Explanation:

.last(): explicitly tell n8n that I only want the last data item. In most cases, this is the safest choice.

.first(): Opposite to .last(), it retrieves the first data item.

.all(): Gets all data items and packs them into a list.

Lesson 3: Retrieving values from a list (array operation)

If the data is a list (such as multiple warehouses), we need to find a specific one through its “house number” (index).

Corresponding node: 3. Operation list data

Objective: Obtain the location of the second warehouse.

Expression: {{ $(product_info_source).last().json.warehouses[1] }}

Explanation:

…warehouses are located in the warehouse list.

[1] Use square brackets to specify the position.

Key point: Counting in the programming world starts from 0! So [0] represents the first one, and [1] represents the second one.

Lesson 4: Deeply nested “folders” (object manipulation)

Data is often nested like folders, and we need to use dots to open them level by level.

Corresponding node: 4. Deep nested structures

Objective: Obtain the contact information of suppliers.

Expression: {{ $(productInfoSource).last().json.supplierInfo.contactPerson }}

Explanation:

…supplierInfo First, let’s enter the supplierInfo layer.

.contactPerson: Find the value of contactPerson within supplierInfo.

Lesson 5: Combined Attacks (Combining Lists and Objects)

This is the most common scenario when dealing with real-world data, such as API returns.

Corresponding node: 5. Combined Punch: Lists and Structures

Objective: Obtain the specific value of the specification “endurance time”.

Expression: {{ $(product_info_source).last().json.specs[1].value }}

Explanation: Let’s proceed in order:

1 specs: First navigate to the specs list.

  • [1]: Find the second member (which is an object containing specName and value) from the list.
  • .value: Extract the value of the ‘value’ field from this object.

Lesson 6: Making Data “Alive” (Operation and Judgment)

Expressions can not only “fetch” data, but also “calculate” data and make judgments.

Corresponding node: 6. Dynamic data processing

Objective 1: Calculate the price including tax (assuming a tax rate of 13%).

Expression: {{ ($(productInfoSource).last().json.price * 1.13).toFixed(2) }}

Explanation: We directly use * for multiplication, and use the .toFixed(2) function to format the result as text with two decimal places retained.

Goal 2: Display in Chinese based on inventory status.

Expression: {{ $(productInformationSource).last().json.inStock ? ‘In stock’ : ‘Out of stock’ }}

Explanation: This is a “ternary operator”, which is a concise way to write if..else. It means: if inStock is

If the value is true, return “In stock”; otherwise, return “Out of stock”.

Lesson 7: Gaining Insight into Unknown Data Structures

This technique is particularly useful when you are unsure about the specific fields contained within an object.

Corresponding node: 7. Insight into data structure

Objective: Dynamically obtain all field names in supplier information.

Expression: {{ Object.keys($(productInfoSource).last().json.supplierInfo) }}

Explanation: Object.keys() will inspect an object and return a list containing all its “keys”.

Lesson 8: Data splitting and recombination

This is a classic operation in data processing, which involves breaking down large tasks into smaller ones and then integrating them back into a whole.

Corresponding node: Split warehouse location -> 8. Aggregate multiple data

Objective: Combine all warehouse locations into one text separated by |.

Expression: {{ $(split_warehouse_locations).all().map(item => item.json.warehouses).join( | ) }}

Explanation:

  • Firstly, the “Split Warehouse Locations” node will split a list containing three warehouses into three separate data items.
  • $(splitWarehouseLocation).all(): Gets these three independent data items.
  • . map() (item=>item. json. warehouses):. map() will traverse each data item, extract the value of warehouses from it, and finally generate a new list [“Shanghai warehouse”, “Shenzhen warehouse”, “Beijing warehouse”].
  • .join( | ): Joins all members of this new list into a string, separating them with |.

🎓 Graduation Assessment: Mastery

Now, please direct your attention to the final node, “Final Learning Outcomes”. It is akin to a final exam, integrating all the aforementioned knowledge points, retrieving data from various preceding nodes, and ultimately compiling a comprehensive and dynamic product report.

Carefully analyze its expression and see if you can understand each part of it.

Congratulations on completing this learning! You have mastered the core skills of n8n expressions, and now you can confidently build your own automated workflow.

#n8n Basic Tutorial