Introduction
In our last session, we mastered the fundamentals of the Set node, learning how to rename fields and map new ones by combining data. You now have the foundational skills to read and reshape data flowing through your workflows. Today, we will apply this knowledge to a much more realistic and common scenario: taming a complex API response.
This lesson directly addresses the goal of transforming a multi-field API response into a simplified, usable output. In the world of CI/CD automation, you'll constantly interact with APIs from tools like GitHub, GitLab, or your build server. These APIs provide a wealth of information, but often in a dense, nested format. Your task will be to extract only the essential details and structure them for the next step in your pipeline, whether that's sending a notification or triggering another action. This lesson will give you the strategy and tools to do just that.
The Challenge of Real-World API Data
When you query an API, the response is typically a comprehensive JSON object designed to serve many different potential needs. It's rarely tailored perfectly to your specific use case. The result is that you often receive a large, nested structure containing far more data than you require.
Your job as an automation engineer is to act as a distiller. You must take this raw, verbose input and transform it into a clean, simple, and flat output that contains only the information relevant to your workflow.
Look at the Set node configuration below. It takes a complex, nested input ($json.response.date, $json.response.task.status) and transforms it into a clean, four-field output. This is precisely the kind of transformation we'll be performing.

A Strategy for Distillation: "Keep Only Set"
In our previous lesson, we learned that the Set node, by default, discards all input fields that you don't explicitly redefine or carry over. We used the "Include Other Input Fields" option to prevent this.
Today, we will embrace this default behavior as a powerful strategy. By not including other fields, you ensure that your output contains only the new, clean fields you've defined. This is the most effective way to guarantee a predictable and simple data structure for downstream nodes. In n8n, this is known as the Keep Only Set option (which is the default when "Include Other Input Fields" is turned off).
This approach is highly recommended when working with API responses to avoid passing along dozens of unnecessary fields that clutter your workflow executions and can even cause confusion later.
Master data transformation with the complete set node guide
This official n8n guide provides excellent tips for using the Set node. It reinforces the strategy we're discussing.
In the "Pro Tips" section, find the list under "Advanced Techniques." Notice the first tip: Use Keep Only Set for API responses. This confirms that our distillation strategy is a best practice.
Accessing Data Beyond the Immediate Input
So far, we've only referenced data from the node immediately preceding our Set node. However, real-world workflows often require you to combine data from multiple, non-adjacent previous steps.
For example, your workflow might:
- Fetch User Info: Get a user's name and ID from a database.
- Fetch Project List: Use the user ID to get a list of their projects from a second API.
- Create Notification: Construct a message that includes the user's name (from step 1) and the project count (from step 2).
To achieve this, n8n allows you to "reach back" to any previous node's output using a special expression syntax. This is analogous to how you might store results in different variables in a Python script and combine them at the end.
The guide below introduces this crucial concept of "backwards referencing."
Reference Data from Previous Nodes in n8n | Node-Bench
This guide from Node-Bench provides a clear explanation and a practical example for referencing data from any node in the workflow's history.
First, read the two sections titled The key expression syntax and Why backwards referencing matters. This will introduce you to the core syntax: $('Node Name').item.json.fieldName. Then, read the syntax breakdown to fully understand how it works.
As you just read, the expression {{ $('Node Name').item.json.fieldName }} allows you to pinpoint data from any specific node, giving you complete freedom to compose your output from multiple sources.
Handling Reality: Missing and Optional Fields
APIs don't always return every field. Some fields might be optional, or their presence might depend on the context of the request. If your expression tries to access a field that doesn't exist, it can lead to errors or undefined values in your output.
From your Python background, you're likely familiar with using dict.get('key', 'default_value') to handle missing dictionary keys gracefully. N8n expressions provide a similar capability using the logical OR (||) or nullish coalescing (??) operators.
{{ $json.optionalField || "default value" }}
This expression will use the value of optionalField if it exists and is not "falsy" (e.g., not null, undefined, false, 0, or an empty string). Otherwise, it will use "default value". The nullish coalescing operator (??) is similar but only provides the default for null or undefined values, which is often more precise.
The following video provides an excellent walkthrough of this and other data transformation techniques.
Master n8n Data Transformation in Just 30 Minutes!
This video from Simon Scrapes, "Master n8n Data Transformation in Just 30 Minutes!", is a great resource for practical data handling.
Watch the section on handling undefined or empty fields. The presenter clearly explains how to use the ?? and || operators to provide default values when data might be missing, which is essential for building robust workflows.
Hands-On Practice: Processing a GitHub Webhook
Now, let's put it all together. We'll simulate processing a payload from a GitHub push event webhook. Your task is to distill this complex object into a simple, clean summary.
-
Create a new, blank workflow.
-
Add a Manual trigger.
-
Add a Set node and connect it to the trigger. This node will act as our mock webhook source. Configure it with the following:
- Mode:
JSON - Paste the following JSON object into the expression box. This represents a simplified GitHub push payload.
{ "ref": "refs/heads/main", "repository": { "name": "n8n-cicd-automation", "full_name": "your-org/n8n-cicd-automation", "owner": { "name": "your-org" } }, "pusher": { "name": "learner", "email": "learner@example.com" }, "head_commit": { "id": "a1b2c3d4e5f6a7b8c9d0e1f2a3b4c5d6e7f8a9b0", "message": "feat: Add new deployment workflow\n\n- Implemented blue/green deployment strategy.\n- Added integration tests.", "timestamp": "2023-10-27T10:00:00Z", "author": { "name": "Learner", "email": "learner@example.com", "username": "n8n-learner" } }, "compare": "https://github.com/your-org/n8n-cicd-automation/compare/a1b2c3d4e5f6...b2c3d4e5f6a7" }- Rename this node to
Mock GitHub Payload.
- Mode:
-
Add a second Set node, connected to the first one. This is where you will perform the transformation. Configure it to create the following simple, flat object:
- Mode:
Manual Mapping - Ensure "Include Other Input Fields" is turned OFF.
- Create the following fields using expressions to pull data from the
Mock GitHub Payloadnode:repoName(String): The name of the repository (e.g.,n8n-cicd-automation).author(String): The username of the commit author (e.g.,n8n-learner).commitHash(String): The first 7 characters of the commit ID. (Hint: JavaScript string methods like.substring()work in expressions. The expression would look something like{{ $json.head_commit.id.substring(0, 7) }}).commitMessage(String): The first line of the commit message. (Hint: You can use.split('\n')[0]to get the first line of a multi-line string).branch(String): The name of the branch, which you need to extract from thereffield. (Hint: Therefisrefs/heads/main. You can use.split('/').pop()to get the last part).
- Mode:
Execute the workflow. The final output of your second Set node should be a single, clean JSON item that looks like this:
{
"repoName": "n8n-cicd-automation",
"author": "n8n-learner",
"commitHash": "a1b2c3d",
"commitMessage": "feat: Add new deployment workflow",
"branch": "main"
}
This exercise consolidates everything we've covered: accessing nested data, renaming fields, using expressions for transformation, and employing the "Keep Only Set" strategy to produce a clean result.
Conclusion
In this lesson, you've taken a significant step from basic data manipulation to practical data engineering within n8n. You now have a robust strategy for handling the complex, verbose data that APIs often provide, a skill that is absolutely critical for your goal of building CI/CD pipelines.
Key Takeaways:
- API responses are often complex and require distillation.
- The
Setnode's default behavior ("Keep Only Set") is the best practice for creating clean, predictable outputs from complex sources. - You can access data from any previous node using the
$('Node Name').item.json.fieldNamesyntax, enabling you to combine information from multiple steps. - Expressions support JavaScript functions (like
.substring()or.split()) for powerful data transformation. - Use the
||or??operators to provide default values and make your workflows more resilient to missing data.
You are now well-equipped to handle the data flow aspect of n8n. In our next lesson, we will introduce control flow. You'll learn to use the IF node to make decisions based on the data you've so carefully prepared, allowing your workflows to execute different actions based on different conditions.