Transforming and cleaning data
1. Transforming and cleaning data
In this video, we'll see why data cleaning and transformation matter, using Acme Events, a part of Acme that collects event signups online.2. Why transformation matters
Users often enter messy data; extra spaces, random capitalization, and typos. Without cleaning, this can lead to duplicate records or login issues. Clean data keeps our automations and analyses reliable.3. Fields and values
As we've seen before, all data in n8n is made up of fields. Each field has a name and a value. For example, in an email record, the field name might be email, and the field value could be [email protected]. This name-value structure is the foundation we'll see across almost all nodes in n8n.4. Static vs. dynamic values
A static value stays the same until we change it, like a date written on a sticky note.5. Static vs. dynamic values
A dynamic value updates automatically, like a clock.6. Static vs. dynamic values
In n8n, dynamic values are defined using expressions, such as {{$json.time}}, which refresh every time the workflow runs.7. What are expressions?
Expressions are the special code snippets written inside curly brackets. When n8n sees one, it runs the code and returns a result. The expressions use data in JSON format from previous nodes, allowing us to reference and transform values dynamically as the workflow runs. For example: {{$json.email.toLowerCase()}} takes the email field from our data and converts it to lowercase. {{$now.format('yyyy-MM-dd')}} gives today's date in a year-month-day format. These are just two simple examples. In reality, n8n supports a wide range of expressions for many different use cases, from formatting dates and text to performing calculations and manipulating data. We'll only look at a few here, but remember that expressions can be applied in many powerful ways.8. Mixing static and dynamic values
We can also mix static and dynamic content in the same field. For example: Hi, I'm Alice and the date is {{$now.format('yyyy-MM-dd')}}. The plain text stays fixed and the expression part changes every time the flow runs.9. Set node: quick fixes with expressions
We might wonder where to use expressions. One common place is the Set node, which we have already seen in previous exercises. The Set node is great for renaming fields, adding new ones, or applying quick transformations with expressions. For example: {{$json.email.trim().toLowerCase()}} cleans an email by trimming spaces and lowercasing it, all in one line. The .trim() and .toLowerCase() methods handle those transformations on $json.email.10. Code node: when it gets more complex
The Set node works for simple fixes, but if we need to clean multiple fields or add custom logic, we use the Code node. For example, imagine Acme Events wants to clean up not just emails, but also attendee names at the same time. With a Code node, we could trim spaces from the email address, convert it to lowercase, and also make sure the first letter of each name is capitalized, all in one step. The Code node lets us write this kind of multi-line logic and return a clean result.11. Set vs Code: when to use which
Here's my rule of thumb: If it's a simple change and we can do it with one expression, use a Set node.12. Set vs Code: when to use which
If it's starting to feel long or we're transforming lots of fields, switch to a Code node: it's more efficient than transforming data using multiple nodes. We'll use both depending on the job.13. Let's practice!
Let's put this new knowledge into practice with an exercise.Create Your Free Account
or
By continuing, you accept our Terms of Use, our Privacy Policy and that your data is stored in the USA.