{"id":16678,"date":"2025-12-17T22:38:31","date_gmt":"2025-12-17T22:38:31","guid":{"rendered":"https:\/\/thinkpeak.ai\/n8n-expressions-variables-guide\/"},"modified":"2025-12-17T22:38:31","modified_gmt":"2025-12-17T22:38:31","slug":"n8n-i%cc%87fadeleri%cc%87-degi%cc%87skenler-kilavuzu","status":"publish","type":"post","link":"https:\/\/thinkpeak.ai\/tr\/n8n-i%cc%87fadeleri%cc%87-degi%cc%87skenler-kilavuzu\/","title":{"rendered":"n8n \u0130fadeler ve De\u011fi\u015fkenler: Pratik K\u0131lavuz"},"content":{"rendered":"<h2>The Mental Model: How n8n &#8220;Thinks&#8221; About Data<\/h2>\n<p>You are staring at the n8n canvas. You\u2019ve successfully connected a Webhook to a Google Sheet. However, there is a problem.<\/p>\n<p>The date format is wrong. The customer name is lowercase. Your <code>IF<\/code> node keeps returning false when it should be true.<\/p>\n<p>You try to fix it. You type a code snippet, but n8n returns an error or <code>undefined<\/code>. Welcome to the <b id=\"expression-editor\">Expression Editor<\/b>.<\/p>\n<p>This is the heartbeat of n8n. While n8n is famous for its visual interface, its true power lies in <b id=\"n8n-expressions\">n8n expressions<\/b> and variables. Understanding this language is vital. It is essentially JavaScript wrapped in a templating engine.<\/p>\n<p>Mastering this skill is the difference between building a simple toy workflow and architecting a scalable enterprise ecosystem. At <a href=\"https:\/\/thinkpeak.ai\">Thinkpeak.ai<\/a>, we build these ecosystems every day.<\/p>\n<p>We rely on advanced expressions to transform static data into dynamic business value. This guide is your manual. We will move beyond the basics and dive deep into the syntax and architecture of <b id=\"variables\">variables<\/b>.<\/p>\n<h2>The Mental Model: How n8n &#8220;Thinks&#8221; About Data<\/h2>\n<p>Before writing code, you must understand the invisible structure holding your data. The main reason expressions fail is a misunderstanding of <b id=\"n8n-data-structure\">n8n\u2019s data structure<\/b>.<\/p>\n<h3>The &#8220;Array of Objects&#8221; Rule<\/h3>\n<p>Every node in n8n receives data in a specific format: <b id=\"array-of-objects\">An Array of Objects<\/b>.<\/p>\n<p>Even if your API returns a simple list, n8n wraps it. Standard JSON looks like a simple object with keys and values. However, n8n places that object inside a <code>json<\/code> key within an array.<\/p>\n<p>When you write an expression, you are usually trying to access data <em>inside<\/em> that <code>json<\/code> key. If you reference the top-level object directly without understanding this wrapper, your workflow will break.<\/p>\n<h3>Item Linking (The &#8220;Magic&#8221; Context)<\/h3>\n<p>n8n tries to be helpful. When you reference a variable like <code>$json.email<\/code>, n8n automatically guesses which &#8220;Item&#8221; in the list you mean.<\/p>\n<p>If you have 10 incoming items, the node runs 10 times. In Run 1, the variable refers to Item 1. In Run 2, it refers to Item 2.<\/p>\n<p>The trap occurs if you break this link using an <b id=\"aggregate-node\">Aggregate node<\/b>. You can no longer use simple shortcodes. You must use specific selectors.<\/p>\n<h2>The Dictionary: Essential n8n Variables and Selectors<\/h2>\n<p>To control your data, you need to know which selector to use. Here is the hierarchy of essential n8n variables.<\/p>\n<h3>1. $json (The Daily Driver)<\/h3>\n<p>This is the shorthand for &#8220;the data inside the current item.&#8221; use this for simple mappings where one input equals one output.<\/p>\n<p><strong>Usage:<\/strong><br \/><code>{{ $json.myField }}<\/code><\/p>\n<h3>2. $node (The Time Traveler)<\/h3>\n<p>This allows you to pull data from <em>any<\/em> previous node in your workflow. It is not limited to the node immediately connected to the current one.<\/p>\n<p><strong>Usage:<\/strong><br \/><code>{{ $node[\"Webhook\"].json.body.order_id }}<\/code><\/p>\n<p><strong>Pro Tip:<\/strong> Always rename your nodes to something descriptive. For example, use &#8220;Get_Apollo_Data&#8221; instead of &#8220;HTTP Request 2&#8221;. It makes your expressions readable and debugging easier.<\/p>\n<h3>3. $input.all() (The Batch Selector)<\/h3>\n<p>This variable accesses <strong>all<\/strong> items arriving at the node as a single array. It does not process them one by one.<\/p>\n<p><strong>Usage:<\/strong><br \/><code>{{ $input.all()[0].json.id }}<\/code><\/p>\n<p>Use this when you need to calculate a sum or aggregate a list of emails. It is also useful when comparing one item against the whole list.<\/p>\n<h3>4. $vars (The Configurator)<\/h3>\n<p>These are global variables you set for the execution. Use this for <b id=\"configuration-settings\">configuration settings<\/b> that might change, like API keys or Channel IDs.<\/p>\n<p><strong>Usage:<\/strong><br \/><code>{{ $vars.slack_channel_id }}<\/code><\/p>\n<h2>Mastering the Expression Editor (JavaScript Logic)<\/h2>\n<p>The n8n Expression Editor accepts <b id=\"javascript-logic\">JavaScript logic<\/b>. This means you aren&#8217;t limited to just selecting fields; you can transform them.<\/p>\n<h3>String Manipulation<\/h3>\n<p>You often need to clean your data before it hits your CRM.<\/p>\n<p><strong>Capitalize First Letter:<\/strong><\/p>\n<pre><code>{{ $json.name.charAt(0).toUpperCase() + $json.name.slice(1) }}<\/code><\/pre>\n<p><strong>Split a Full Name:<\/strong><\/p>\n<pre><code>\/\/ First Name\n{{ $json.fullName.split(\" \")[0] }}\n\/\/ Last Name\n{{ $json.fullName.split(\" \").slice(1).join(\" \") }}<\/code><\/pre>\n<p><strong>Regex Extraction:<\/strong><\/p>\n<pre><code>{{ $json.subject.match(\/Order #(d+)\/)[1] }}<\/code><\/pre>\n<p>Complex string manipulation is often required for scraping and enrichment. Our <b id=\"cold-outreach-tools\">Cold Outreach tools<\/b> use advanced regex patterns to scrape prospect data. Instead of building these from scratch, you can deploy our pre-architected systems at <a href=\"https:\/\/thinkpeak.ai\">Thinkpeak.ai<\/a>.<\/p>\n<h3>Math and Numbers<\/h3>\n<p><strong>Rounding Currency:<\/strong><\/p>\n<pre><code>{{ Math.round($json.price * 100) \/ 100 }}<\/code><\/pre>\n<p><strong>Calculating Margins:<\/strong><\/p>\n<pre><code>{{ ($json.revenue - $json.cost) \/ $json.revenue * 100 }}%<\/code><\/pre>\n<h3>Logic (Ternary Operators)<\/h3>\n<p>You can replace entire <code>IF<\/code> nodes with a single line of code. This cleans up your canvas significantly.<\/p>\n<p><strong>Syntax:<\/strong> <code>condition ? value_if_true : value_if_false<\/code><\/p>\n<p><strong>Example:<\/strong><\/p>\n<pre><code>{{ $json.deal_size > 1000 ? \"High Priority\" : \"Standard\" }}<\/code><\/pre>\n<h2>Date &#038; Time Manipulation with Luxon<\/h2>\n<p>Dates are the nemesis of automation. Different APIs use different formats. n8n uses a library called <b id=\"luxon\">Luxon<\/b> to handle this.<\/p>\n<h3>The $now and $today Objects<\/h3>\n<ul>\n<li><code>$now<\/code>: Returns the current date and time in ISO format.<\/li>\n<li><code>$today<\/code>: Returns the current date with the time set to 00:00:00.<\/li>\n<\/ul>\n<h3>Common Luxon Formulas<\/h3>\n<p><strong>Format a Date (DD\/MM\/YYYY):<\/strong><\/p>\n<pre><code>{{ $now.toFormat('dd\/MM\/yyyy') }}<\/code><\/pre>\n<p><strong>Calculate a Deadline (Today + 7 Days):<\/strong><\/p>\n<pre><code>{{ $now.plus({ days: 7 }).toISO() }}<\/code><\/pre>\n<p><strong>Parse a Weird Format:<\/strong><\/p>\n<p>If an API sends &#8220;12-25-2025&#8221;, n8n might treat it as a string. You must parse it to do math on it.<\/p>\n<pre><code>{{ DateTime.fromFormat($json.dateString, 'MM-dd-yyyy').toFormat('yyyy-MM-dd') }}<\/code><\/pre>\n<p>Timezone errors can kill a marketing campaign. We strictly use <b id=\"iso-8601-formats\">ISO 8601 formats<\/b> internally at <a href=\"https:\/\/thinkpeak.ai\">Thinkpeak.ai<\/a>. This ensures our AI agents never miss a beat across different time zones.<\/p>\n<h2>When Expressions Aren&#8217;t Enough: The Code Node<\/h2>\n<p>Expressions are great for one-liners. But sometimes you find yourself writing nested ternary operators. Or, you might be trying to filter complex arrays inside the Expression Editor.<\/p>\n<p>It is time to upgrade to the <b id=\"code-node\">Code Node<\/b>. This node allows you to write full multi-line JavaScript or Python.<\/p>\n<p><strong>Example: Deduplicating a Complex List<\/strong><\/p>\n<p>You can&#8217;t easily remove duplicates based on a specific sub-property using just expressions.<\/p>\n<pre><code>\/\/ Code Node Example\nconst seen = new Set();\nreturn $input.all().filter(item => {\n  const duplicate = seen.has(item.json.email);\n  seen.add(item.json.email);\n  return !duplicate;\n});<\/code><\/pre>\n<h3>The &#8220;Limitless&#8221; Tier<\/h3>\n<p>This is where <a href=\"https:\/\/thinkpeak.ai\">Thinkpeak.ai<\/a> shines. Standard expressions cover most use cases. The remaining 20% involves <b id=\"complex-business-logic\">complex business logic<\/b> and API pagination loops.<\/p>\n<p>Our Bespoke Engineering service handles this heavy lifting. We build entire backend architectures. If you need a <b id=\"custom-ai-agent\">Custom AI Agent<\/b> capable of reasoning, we build the infrastructure to support it.<\/p>\n<h2>Debugging Your Expressions<\/h2>\n<p>Even the best architects face errors. Here is how to resolve the most common ones.<\/p>\n<h3>1. [Object object] Output<\/h3>\n<p><strong>Cause:<\/strong> You are trying to print an entire object into a text field that expects a string.<\/p>\n<p><strong>Fix:<\/strong> Access the specific field like <code>$json.name<\/code>. Alternatively, you can stringify the object.<\/p>\n<h3>2. undefined<\/h3>\n<p><strong>Cause:<\/strong> The field you are asking for doesn&#8217;t exist in the data for that specific item.<\/p>\n<p><strong>Fix:<\/strong> Use the <b id=\"optional-chaining\">Optional Chaining<\/b> operator (<code>?.<\/code>). For example, <code>{{ $json.address?.zipcode || \"No Zip\" }}<\/code> returns &#8220;No Zip&#8221; if the field is missing.<\/p>\n<h3>3. &#8220;Referenced Node is not ancestor&#8221;<\/h3>\n<p><strong>Cause:<\/strong> You are trying to use <code>$node<\/code> to access data from a branch that isn&#8217;t connected.<\/p>\n<p><strong>Fix:<\/strong> Connect the nodes directly. You can also use a Merge node to bring the data streams together.<\/p>\n<h2>Conclusion<\/h2>\n<p>Mastering expressions and variables takes you from &#8220;tinkerer&#8221; to &#8220;automation architect.&#8221; You can force n8n to do exactly what you want.<\/p>\n<p>You simply need to understand the Array of Objects structure. Leverage the power of Luxon for dates. Utilize JavaScript for logic.<\/p>\n<p>At <a href=\"https:\/\/thinkpeak.ai\">Thinkpeak.ai<\/a>, we help businesses skip the learning curve. Check out our Automation Marketplace for pre-architected workflows.<\/p>\n<p>If you need scale, partner with our Bespoke Engineering team. We build custom low-code apps that integrate your entire stack. Transform your business into a self-driving ecosystem today.<\/p>\n<p><a href=\"https:\/\/thinkpeak.ai\">Explore the Thinkpeak.ai Automation Marketplace<\/a><\/p>\n<h2>Frequently Asked Questions (FAQ)<\/h2>\n<h3>What is the difference between $json and $input.item?<\/h3>\n<p><code>$json<\/code> is actually a shorthand alias for <code>$input.item.json<\/code>. They access the exact same data. <code>$json<\/code> is preferred for brevity. However, <code>$input.item<\/code> is useful if you also need to access the binary data associated with that item.<\/p>\n<h3>How do I access data from two nodes ago?<\/h3>\n<p>You use the <code>$node<\/code> selector. For example: <code>{{ $node[\"Name of Node\"].json.field }}<\/code>. Note that the node name must be exact and case-sensitive. If the node name has spaces, use brackets and quotes.<\/p>\n<h3>Why does my date expression return an error?<\/h3>\n<p>n8n dates are often strings. You must convert them to a <b id=\"luxon-object\">Luxon object<\/b> before you can modify them. Use <code>DateTime.fromISO()<\/code> or <code>DateTime.fromFormat()<\/code> first.<\/p>\n<h2>Resources<\/h2>\n<ul>\n<li><a href=\"https:\/\/docs.n8n.io\/code\/expressions\/\" rel=\"nofollow noopener\" target=\"_blank\">https:\/\/docs.n8n.io\/code\/expressions\/<\/a><\/li>\n<li><a href=\"https:\/\/docs.n8n.io\/code\/builtin\/data-transformation-functions\/\" rel=\"nofollow noopener\" target=\"_blank\">https:\/\/docs.n8n.io\/code\/builtin\/data-transformation-functions\/<\/a><\/li>\n<li><a href=\"https:\/\/docs.n8n.io\/code\/cookbook\/expressions\/\" rel=\"nofollow noopener\" target=\"_blank\">https:\/\/docs.n8n.io\/code\/cookbook\/expressions\/<\/a><\/li>\n<li><a href=\"https:\/\/docs.n8n.io\/code\/code-node\/\" rel=\"nofollow noopener\" target=\"_blank\">https:\/\/docs.n8n.io\/code\/code-node\/<\/a><\/li>\n<li><a href=\"https:\/\/docs.n8n.io\/code\/variables\/\" rel=\"nofollow noopener\" target=\"_blank\">https:\/\/docs.n8n.io\/code\/variables\/<\/a><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>\u0130\u015f ak\u0131\u015flar\u0131n\u0131 d\u00fczeltmek i\u00e7in n8n ifadelerini, de\u011fi\u015fkenleri, Luxon tarihlerini ve hata ay\u0131klamay\u0131 kullanmaya y\u00f6nelik a\u00e7\u0131k ve pratik ad\u0131mlar \u00f6\u011frenin.<\/p>","protected":false},"author":2,"featured_media":16677,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[103],"tags":[],"class_list":["post-16678","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-business-process-automation"],"_links":{"self":[{"href":"https:\/\/thinkpeak.ai\/tr\/wp-json\/wp\/v2\/posts\/16678","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/thinkpeak.ai\/tr\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/thinkpeak.ai\/tr\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/thinkpeak.ai\/tr\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/thinkpeak.ai\/tr\/wp-json\/wp\/v2\/comments?post=16678"}],"version-history":[{"count":0,"href":"https:\/\/thinkpeak.ai\/tr\/wp-json\/wp\/v2\/posts\/16678\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/thinkpeak.ai\/tr\/wp-json\/wp\/v2\/media\/16677"}],"wp:attachment":[{"href":"https:\/\/thinkpeak.ai\/tr\/wp-json\/wp\/v2\/media?parent=16678"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/thinkpeak.ai\/tr\/wp-json\/wp\/v2\/categories?post=16678"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/thinkpeak.ai\/tr\/wp-json\/wp\/v2\/tags?post=16678"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}