Introduction
In our last session, you successfully configured a GitHub Trigger node to listen for push events, creating a direct line of communication from your repository to your n8n workflow. When you tested it, you saw the complete, and rather large, JSON payload that GitHub sends. This raw data is the lifeblood of our automation, but in its raw state, it's not very useful.
This lesson focuses on the crucial next step: parsing that complex payload to extract the specific pieces of information we need. You will learn how to navigate the JSON structure and pull out key details like the commit message, author, and branch name. Mastering this skill is fundamental to building any meaningful CI/CD workflow, as it allows you to transform raw event data into actionable intelligence.
Understanding the Webhook Payload Structure
Before you can extract data, you need to know where to find it. When a trigger node like the GitHub Trigger or a generic Webhook node receives data, it places it into the n8n data structure as a JSON object. However, the exact location of the payload can differ based on the node you use.
- Specialized Trigger Nodes (
GitHub Trigger,GitLab Trigger, etc.): These nodes are designed for a specific service. They often "unwrap" the payload for you, placing the main body of the event data directly into thejsonobject of the incoming item. - Generic
WebhookNode: This node is a general-purpose listener. It typically places the entire body of the incoming HTTP request into a nestedbodyobject within the mainjsonobject.
Therefore, if you're using a GitHub Trigger, you might access the branch reference with an expression like {{ $json.ref }}. If you were using a generic Webhook node for the same event, you would likely need to use {{ $json.body.ref }}. This is a common point of confusion, and being aware of it will save you significant debugging time.
Let's look at a practical example of how these fields are extracted.
This article by Marcin Kujawski demonstrates a workflow that monitors a repository and sends email alerts. It provides excellent examples of extracting the exact fields we're interested in for CI/CD automation. The author uses a generic Webhook node, so pay close attention to the data paths.
Focus on steps 2 and 3 under the heading "2️⃣ Build GitHub Monitoring Workflow in N8N". First, review Add Branch Filter. Notice the expression {{ $json.body.ref }}. This is used to get the branch name. Next, examine the email configuration under Add Email Notification. The author extracts several critical pieces of data: repository.full_name pusher.name and pusher.email head_commit.message, head_commit.url, and head_commit.id Since you are using the specialized GitHub Trigger node from our last lesson, you would access these fields without the body part of the path (e.g., {{ $json.ref }}, {{ $json.repository.full_name }}). Method 1: Parsing with n8n Expressions The most direct way to parse data in n8n is using its built-in expression language. Expressions are small snippets of code enclosed in double curly braces, {{ }}, that allow you to reference data from previous nodes. You use dot notation to navigate the JSON object hierarchy. Let's apply this to the workflow you built in the last lesson. Open the workflow containing your GitHub Trigger node. After the trigger, add a Set node. The Set node is perfect for creating new fields or restructuring existing data. In the Set node's parameters, we will extract the key fields from the GitHub payload. Configure it as follows: Click Add Value. Set the Name to commit_message. For the Value, click the "Add Expression" icon (fx) and enter: {{ $json.head_commit.message }} Repeat this process for other valuable fields: Name: branch, Value: {{ $json.ref }} Name: author, Value: {{ $json.pusher.name }} Name: commit_url, Value: {{ $json.head_commit.url }} Name: repo, Value: {{ $json.repository.full_name }} Now, re-run the test from the GitHub Trigger node. After it receives the data, the Set node will execute. When you inspect the output of the Set node, you will see a clean, simplified JSON object containing only the fields you explicitly extracted. Method 2: Parsing with the Python Code Node While expressions are great for direct data mapping, your Python expertise opens up a more powerful and flexible option: the Code node. This is ideal for more complex transformations, data validation, or simply working in a familiar environment.
Since you are using the specialized GitHub Trigger node from our last lesson, you would access these fields without the body part of the path (e.g., {{ $json.ref }}, {{ $json.repository.full_name }}).
Method 1: Parsing with n8n Expressions
The most direct way to parse data in n8n is using its built-in expression language. Expressions are small snippets of code enclosed in double curly braces, {{ }}, that allow you to reference data from previous nodes. You use dot notation to navigate the JSON object hierarchy.
Let's apply this to the workflow you built in the last lesson.
-
Open the workflow containing your
GitHub Triggernode. -
After the trigger, add a Set node. The Set node is perfect for creating new fields or restructuring existing data.
-
In the Set node's parameters, we will extract the key fields from the GitHub payload. Configure it as follows:
- Click Add Value.
- Set the Name to
commit_message. - For the Value, click the "Add Expression" icon (
fx) and enter:{{ $json.head_commit.message }} - Repeat this process for other valuable fields:
- Name:
branch, Value:{{ $json.ref }} - Name:
author, Value:{{ $json.pusher.name }} - Name:
commit_url, Value:{{ $json.head_commit.url }} - Name:
repo, Value:{{ $json.repository.full_name }}
- Name:
-
Now, re-run the test from the
GitHub Triggernode. After it receives the data, theSetnode will execute. When you inspect the output of theSetnode, you will see a clean, simplified JSON object containing only the fields you explicitly extracted.
Method 2: Parsing with the Python Code Node
While expressions are great for direct data mapping, your Python expertise opens up a more powerful and flexible option: the Code node. This is ideal for more complex transformations, data validation, or simply working in a familiar environment.
Python Code Nodes - n8n Skills - Mintlify
This guide provides essential patterns for working with Python in n8n. It covers accessing data, safe parsing techniques, and common pitfalls.
First, read the section Webhook Data Structure. This reinforces the concept of the nested body object when using a generic Webhook node. Next, review the error patterns section, paying close attention to KeyError on dictionary access and Webhook body nesting. Using the .get() method is a robust way to prevent your workflow from failing if a field is unexpectedly missing from the payload. Finally, look at the Best Practices section, which summarizes these safe-access patterns.
Let's replace the Set node with a Code node to achieve the same result.
- Disable or delete the
Setnode from your workflow. - Add a Code node and set its language to Python.
- The incoming data is available through the
_inputvariable. Since the trigger node outputs a single item, we can access its JSON data with_input.first()['json']. - Enter the following Python code. It uses the safe
.get()method to extract the data and constructs a new dictionary, which it then returns.
# Access the incoming JSON payload from the GitHub Trigger
incoming_data = _input.first()['json']
# Safely extract key fields using .get() to avoid errors
# The second argument to .get() is a default value if the key is not found
ref = incoming_data.get('ref', 'N/A')
repo_full_name = incoming_data.get('repository', {}).get('full_name', 'N/A')
pusher_name = incoming_data.get('pusher', {}).get('name', 'N/A')
# For nested objects like head_commit, chain .get() calls
head_commit = incoming_data.get('head_commit', {})
commit_message = head_commit.get('message', 'No commit message')
commit_url = head_commit.get('url', '#')
# Construct the new, simplified item
parsed_commit_info = {
"repo": repo_full_name,
"branch": ref,
"author": pusher_name,
"commit_message": commit_message,
"commit_url": commit_url
}
# n8n expects a list of items to be returned.
# Each item must be a dictionary with a "json" key.
return [{"json": parsed_commit_info}]
Execute your workflow again. The output of the Code node should be a single JSON object, identical in structure to the one you created with the Set node. You have successfully parsed the webhook payload using two different, powerful methods.
Conclusion
In this lesson, you transformed a raw, complex Git webhook payload into a clean, structured, and usable piece of information. This is a critical skill for developing automated CI/CD pipelines in n8n.
Key Takeaways:
- Webhook payloads from different trigger nodes can have different structures (e.g.,
jsonvs.json.body). - N8n expressions with dot notation (
{{ $json.field.subfield }}) provide a quick way to extract data. - The Set node is excellent for simple data mapping and transformation using expressions.
- The Code node with Python offers maximum flexibility and power for parsing, especially when using safe-access patterns like
.get()to handle variations in the payload.
You now have the parsed data you need. In the next lesson, we will put this data to work by building a workflow that takes your parsed commit summary and automatically posts it as a notification to a chat channel, completing your first CI/CD feedback loop.