3、JSON处理( n8n 节点用于交换信息的语言)

工作原理

这个工作流是一个交互式、动手教程,旨在教你 JSON(JavaScript 对象表示法)的绝对基础知识,更重要的是,如何在 n8n 中使用它。它非常适合刚开始接触自动化和数据结构的初学者。

教程以一系列简单的步骤构建。每个节点介绍 JSON 的一个新基本概念:

  1. 键/值对:所有 JSON 的基本构建块。

  2. 数据类型:然后逐一为您介绍最常见的数据类型:

    • 字符串 (String)(文本)
    • 数字 (Number)(整数和小数)
    • 布尔值 (Boolean)truefalse
    • 空值 (Null)(表示"无")
    • 数组 (Array)(一个有序的项目列表)
    • 对象(Object)(一个键/值对的集合)
  3. 使用表达式与 JSON :最关键的一步!它展示了如何使用 n8n 的表达式( {{ }} )将数据从上一个节点动态地提取到新节点中。

  4. 期末考试:一个最终的节点将所有内容整合在一起,通过引用所有先前步骤中的数据来构建一个完整的 JSON 对象。

  5. 节点可通过数据转换 -> Edit Fields 添加

什么是JSON

JSON是应用程序和 n8n 节点用于交换信息的语言。

1. JSON 的核心:键 & 值

JSON 中的一切都是基于这一对元素构建的:

  • 键(Key) :数据的名称,始终用双引号 " 包裹。
  • 值(Value):数据本身。

"key": "value"

在此节点的输出中,你会看到两个键/值对。这是后续所有内容的基本构建单元。

2. 数据类型:字符串(String)

字符串就是文本

  • 语法: 文本始终用双引号 " " 包裹。

查看输出:json_example_string 的值就是我们定义的文本。

3. 数据类型:数字

数字就是数值,可以是整数(integer)如 10,也可以是小数(float)如 12.5。

  • 语法: 直接写数字,不要加引号

"age": 30(正确) "age": "30"(错误,这是字符串!)

这种区分在进行数学计算时至关重要!

4. 数据类型:布尔值()

布尔值只能是 TRUEFALSE

  • 语法: truefalse(始终小写,且不加引号)。

可以把它想象成电灯开关:开(true)或关(false)。它在条件判断(If/Then 逻辑)中非常有用。

5. 数据类型:空值(Null)

这种特殊类型表示"无"、"没有值"或"空"。

  • 语法: null(小写,且不加引号)。

它不同于 0(数字)或 ""(空字符串)。null 表示有意的"没有值"。

6. 数据类型:数组(Array)

数组是一个有序的项目列表

  • 语法:[ 开头,以 ] 结尾,项目之间用逗号分隔。

数组可以包含任何内容:字符串、数字、布尔值,甚至其他数组或对象!

7. 数据类型:对象(Object / JSON 对象)

这是最核心的概念!对象是键/值对的集合

  • 语法:{ 开头,以 } 结尾。

对象让我们能够组织复杂的数据,就像一开始的联系人卡片。注意这个对象包含了我们之前看到的所有其他数据类型!

8. ⭐ 关键步骤:在 n8n 中使用 JSON!

现在到了魔法环节。如何使用来自上一个节点的数据?使用 表达式 {{ }}

此节点创建了一个自定义消息。看看 message 字段的值(通过拖动字段实现引用 ): Hello, the number from the tutorial is: {{ $('Number').item.json.json_example_integer }}

它动态地从 "Number" 节点中获取了数字 10!这就是让各节点互相传递数据的方法。

9. 🎓 期末考试:综合应用

最后这个节点通过表达式从所有前置节点获取数据,创建了一个最终对象。

点击此节点,查看每个字段中的表达式。这是对你所学内容的完美总结。

其他

引用地址:5170-learn-json-basics-with-an-interactive-step-by-step-tutorial-for-beginners

代码如下,可保存到文件里,然后导入到画布。

json 复制代码
{
  "name": "3、JSON处理",
  "nodes": [
    {
      "parameters": {},
      "id": "34decf3c-5e90-4d4f-a5d6-fce46a5fdb1b",
      "cid": "Ikx1Y2FzIFBleXJpbiI",
      "name": "Execute to Start",
      "type": "n8n-nodes-base.manualTrigger",
      "creator": "Lucas Peyrin",
      "position": [
        -832,
        832
      ],
      "typeVersion": 1,
      "notes": "© 2025 Lucas Peyrin"
    },
    {
      "parameters": {
        "assignments": {
          "assignments": [
            {
              "id": "e87952cb-878e-4feb-8261-342eaf887838",
              "name": "json_example_string",
              "type": "string",
              "value": "This is a simple string. In JSON, it's always enclosed in double quotes."
            }
          ]
        },
        "options": {}
      },
      "id": "5b401d73-b90c-4740-8e26-ff3cc774e179",
      "cid": "Ikx1Y2FzIFBleXJpbiI",
      "name": "String",
      "type": "n8n-nodes-base.set",
      "creator": "Lucas Peyrin",
      "position": [
        -192,
        832
      ],
      "typeVersion": 3.4,
      "notes": "© 2025 Lucas Peyrin"
    },
    {
      "parameters": {
        "assignments": {
          "assignments": [
            {
              "id": "e87952cb-878e-4feb-8261-342eaf887838",
              "name": "key",
              "type": "string",
              "value": "value"
            },
            {
              "id": "b5f030f4-6650-4181-881f-de44790bb24b",
              "name": "another_key",
              "type": "string",
              "value": "another_value"
            }
          ]
        },
        "options": {}
      },
      "id": "7b7da1a2-c4a4-498d-814a-1a684d1b664e",
      "cid": "Ikx1Y2FzIFBleXJpbiI",
      "name": "Key & Value",
      "type": "n8n-nodes-base.set",
      "creator": "Lucas Peyrin",
      "position": [
        -480,
        832
      ],
      "typeVersion": 3.4,
      "notes": "© 2025 Lucas Peyrin"
    },
    {
      "parameters": {
        "assignments": {
          "assignments": [
            {
              "id": "e87952cb-878e-4feb-8261-342eaf887838",
              "name": "json_example_integer",
              "type": "number",
              "value": 10
            },
            {
              "id": "12345",
              "name": "json_example_float",
              "type": "number",
              "value": 12.5
            }
          ]
        },
        "options": {}
      },
      "id": "83747f70-9d1d-4c45-b03a-d19e819085bb",
      "cid": "Ikx1Y2FzIFBleXJpbiI",
      "name": "Number",
      "type": "n8n-nodes-base.set",
      "creator": "Lucas Peyrin",
      "position": [
        96,
        832
      ],
      "typeVersion": 3.4,
      "notes": "© 2025 Lucas Peyrin"
    },
    {
      "parameters": {
        "assignments": {
          "assignments": [
            {
              "id": "e87952cb-878e-4feb-8261-342eaf887838",
              "name": "json_example_boolean",
              "type": "boolean",
              "value": false
            }
          ]
        },
        "options": {}
      },
      "id": "d5255f9d-6706-4909-a7d3-b347eac069f2",
      "cid": "Ikx1Y2FzIFBleXJpbiI",
      "name": "Boolean",
      "type": "n8n-nodes-base.set",
      "creator": "Lucas Peyrin",
      "position": [
        384,
        832
      ],
      "typeVersion": 3.4,
      "notes": "© 2025 Lucas Peyrin"
    },
    {
      "parameters": {
        "assignments": {
          "assignments": [
            {
              "id": "e87952cb-878e-4feb-8261-342eaf887838",
              "name": "json_example_array",
              "type": "array",
              "value": "[\"first element\", 2, false, null]"
            }
          ]
        },
        "options": {}
      },
      "id": "35d5a25c-e69f-4905-a279-7508a8d7e3c6",
      "cid": "Ikx1Y2FzIFBleXJpbiI",
      "name": "Array",
      "type": "n8n-nodes-base.set",
      "creator": "Lucas Peyrin",
      "position": [
        960,
        832
      ],
      "typeVersion": 3.4,
      "notes": "© 2025 Lucas Peyrin"
    },
    {
      "parameters": {
        "assignments": {
          "assignments": [
            {
              "id": "e87952cb-878e-4feb-8261-342eaf887838",
              "name": "json_example_object",
              "type": "object",
              "value": "{\"key\":\"value\",\"array\":[1,2,3],\"boolean\":false,\"integer\":123,\"sub_object\":{\"sub_key\":\"Find me!\"}}"
            }
          ]
        },
        "options": {}
      },
      "id": "9ffe5850-8cb4-4f2d-9be4-bfa037b2d43e",
      "cid": "Ikx1Y2FzIFBleXJpbiI",
      "name": "Object",
      "type": "n8n-nodes-base.set",
      "creator": "Lucas Peyrin",
      "position": [
        1264,
        832
      ],
      "typeVersion": 3.4,
      "notes": "© 2025 Lucas Peyrin"
    },
    {
      "parameters": {
        "content": "## 教程 - 什么是 JSON?\n\n欢迎!此工作流将向你介绍 JSON 的基础知识------这是应用程序和 n8n 节点用于交换信息的语言。\n\n**什么是 JSON?**\n想象一张联系人卡片:\n\n- **Name:** John Doe\n- **Age:** 30\n- **Has Children:** Yes\n- **Phone Numbers:** [\"555-1234\", \"555-5678\"]\n\nJSON 只是将这些信息写成一种计算机可以完全理解的方式。\n\n**如何使用本教程:**\n\n1. 点击 **"Execute Workflow"** 按钮。\n2. 按顺序一个个点击每个节点。\n3. 在右侧面板查看节点的输出,并阅读对应的便利贴以理解发生了什么。\n",
        "height": 656,
        "width": 460
      },
      "id": "782a119e-d6da-47de-8f2d-22350ff749f7",
      "cid": "Ikx1Y2FzIFBleXJpbiI",
      "name": "Sticky Note",
      "type": "n8n-nodes-base.stickyNote",
      "creator": "Lucas Peyrin",
      "position": [
        -1056,
        336
      ],
      "typeVersion": 1,
      "notes": "© 2025 Lucas Peyrin"
    },
    {
      "parameters": {
        "content": "#### JSON 的核心:键 & 值\n\nJSON 中的一切都是基于这一对元素构建的:\n\n* **键(Key)**:数据的名称,始终用双引号 `\"` 包裹。\n* **值(Value)**:数据本身。\n\n`\"key\": \"value\"`\n\n在此节点的输出中,你会看到两个键/值对。这是后续所有内容的基本构建单元。\n",
        "height": 516,
        "width": 260,
        "color": 7
      },
      "id": "a87480ab-2e3c-47de-96c2-c385bfb1e2d8",
      "cid": "Ikx1Y2FzIFBleXJpbiI",
      "name": "Sticky Note1",
      "type": "n8n-nodes-base.stickyNote",
      "creator": "Lucas Peyrin",
      "position": [
        -560,
        480
      ],
      "typeVersion": 1,
      "notes": "© 2025 Lucas Peyrin"
    },
    {
      "parameters": {
        "content": "#### 数据类型:字符串\n\n字符串就是**文本**。\n\n* **语法:** 文本始终用双引号 `\" \"` 包裹。\n\n查看输出:`json_example_string` 的值就是我们定义的文本。\n",
        "height": 516,
        "width": 260,
        "color": 7
      },
      "id": "65adc063-ae9d-4e7a-9730-e4cacad42b0f",
      "cid": "Ikx1Y2FzIFBleXJpbiI",
      "name": "Sticky Note2",
      "type": "n8n-nodes-base.stickyNote",
      "creator": "Lucas Peyrin",
      "position": [
        -272,
        480
      ],
      "typeVersion": 1,
      "notes": "© 2025 Lucas Peyrin"
    },
    {
      "parameters": {
        "content": "#### 数据类型:数字\n\n数字就是数值,可以是整数(integer)如 10,也可以是小数(float)如 12.5。\n\n* **语法:** 直接写数字,**不要加引号**。\n\n`\"age\": 30`(正确)\n`\"age\": \"30\"`(错误,这是字符串!)\n\n这种区分在进行数学计算时至关重要!\n",
        "height": 516,
        "width": 260,
        "color": 7
      },
      "id": "3f67ebc2-9ce3-468e-810a-1265c73c07ca",
      "cid": "Ikx1Y2FzIFBleXJpbiI",
      "name": "Sticky Note3",
      "type": "n8n-nodes-base.stickyNote",
      "creator": "Lucas Peyrin",
      "position": [
        16,
        480
      ],
      "typeVersion": 1,
      "notes": "© 2025 Lucas Peyrin"
    },
    {
      "parameters": {
        "content": "#### 数据类型:布尔值\n\n布尔值只能是 **TRUE** 或 **FALSE**。\n\n* **语法:** `true` 或 `false`(始终小写,且**不加引号**)。\n\n可以把它想象成电灯开关:开(`true`)或关(`false`)。它在条件判断(If/Then 逻辑)中非常有用。\n",
        "height": 516,
        "width": 260,
        "color": 7
      },
      "id": "5637dcaf-463e-4fa2-b91c-3bb721acef24",
      "cid": "Ikx1Y2FzIFBleXJpbiI",
      "name": "Sticky Note4",
      "type": "n8n-nodes-base.stickyNote",
      "creator": "Lucas Peyrin",
      "position": [
        304,
        480
      ],
      "typeVersion": 1,
      "notes": "© 2025 Lucas Peyrin"
    },
    {
      "parameters": {
        "content": "#### 数据类型:数组(Array)\n\n数组是一个**有序的项目列表**。\n\n* **语法:** 以 `[` 开头,以 `]` 结尾,项目之间用逗号分隔。\n\n数组可以包含任何内容:字符串、数字、布尔值,甚至其他数组或对象!\n",
        "height": 516,
        "width": 260,
        "color": 7
      },
      "id": "cc4b20f5-fb5a-4074-91ae-da80841e3ec3",
      "cid": "Ikx1Y2FzIFBleXJpbiI",
      "name": "Sticky Note5",
      "type": "n8n-nodes-base.stickyNote",
      "creator": "Lucas Peyrin",
      "position": [
        880,
        480
      ],
      "typeVersion": 1,
      "notes": "© 2025 Lucas Peyrin"
    },
    {
      "parameters": {
        "content": "#### 数据类型:对象(Object / JSON 对象)\n\n这是最核心的概念!对象是**键/值对的集合**。\n\n* **语法:** 以 `{` 开头,以 `}` 结尾。\n\n对象让我们能够组织复杂的数据,就像一开始的联系人卡片。注意这个对象包含了我们之前看到的所有其他数据类型!",
        "height": 516,
        "width": 280,
        "color": 7
      },
      "id": "4f5ca1ef-212a-4dfb-ab2a-40b531ad36ec",
      "cid": "Ikx1Y2FzIFBleXJpbiI",
      "name": "Sticky Note6",
      "type": "n8n-nodes-base.stickyNote",
      "creator": "Lucas Peyrin",
      "position": [
        1168,
        480
      ],
      "typeVersion": 1,
      "notes": "© 2025 Lucas Peyrin"
    },
    {
      "parameters": {
        "assignments": {
          "assignments": [
            {
              "id": "e87952cb-878e-4feb-8261-342eaf887838",
              "name": "json_example_null",
              "type": "null",
              "value": {}
            }
          ]
        },
        "options": {}
      },
      "id": "40791faa-008d-4a7f-9b0a-57681208076c",
      "cid": "Ikx1Y2FzIFBleXJpbiI",
      "name": "Null",
      "type": "n8n-nodes-base.set",
      "creator": "Lucas Peyrin",
      "position": [
        672,
        832
      ],
      "typeVersion": 3.4,
      "notes": "© 2025 Lucas Peyrin"
    },
    {
      "parameters": {
        "content": "#### 数据类型:空值(Null)\n\n这种特殊类型表示"无"、"没有值"或"空"。\n\n* **语法:** `null`(小写,且**不加引号**)。\n\n它不同于 `0`(数字)或 `\"\"`(空字符串)。`null` 表示有意的"没有值"。\n",
        "height": 516,
        "width": 260,
        "color": 7
      },
      "id": "cbdda850-669b-42e0-b89a-a1027ec7e75b",
      "cid": "Ikx1Y2FzIFBleXJpbiI",
      "name": "Sticky Note7",
      "type": "n8n-nodes-base.stickyNote",
      "creator": "Lucas Peyrin",
      "position": [
        592,
        480
      ],
      "typeVersion": 1,
      "notes": "© 2025 Lucas Peyrin"
    },
    {
      "parameters": {
        "assignments": {
          "assignments": [
            {
              "id": "e87952cb-878e-4feb-8261-342eaf887838",
              "name": "message",
              "type": "string",
              "value": "=Hello, the number from the tutorial is: {{ $('Number').item.json.json_example_integer }}"
            },
            {
              "id": "61f385f4-b8e2-4c69-b873-9ffc3ab3fe94",
              "name": "sub_key",
              "type": "string",
              "value": "={{ $json.json_example_object.sub_object.sub_key }}"
            },
            {
              "id": "bd752a0f-64bf-44b1-b39b-fca28e86aa5b",
              "name": "array_second_item",
              "type": "string",
              "value": "={{ $json.json_example_object.array[1] }}"
            }
          ]
        },
        "options": {}
      },
      "id": "8e1368af-92d9-42b9-a69e-68af489cee58",
      "cid": "Ikx1Y2FzIFBleXJpbiI",
      "name": "Using JSON (Expressions)",
      "type": "n8n-nodes-base.set",
      "creator": "Lucas Peyrin",
      "position": [
        1600,
        832
      ],
      "typeVersion": 3.4,
      "notes": "© 2025 Lucas Peyrin"
    },
    {
      "parameters": {
        "content": "#### ⭐ 关键步骤:在 n8n 中使用 JSON!\n\n现在到了魔法环节。如何使用来自上一个节点的数据?使用 **表达式** `{{ }}`。\n\n此节点创建了一个自定义消息。看看 `message` 字段的值:\n`Hello, the number from the tutorial is: {{ $('Number').item.json.json_example_integer }}`\n\n它动态地从 "Number" 节点中获取了数字 `10`!这就是让各节点互相传递数据的方法。\n",
        "height": 516,
        "width": 340,
        "color": 5
      },
      "id": "d3752623-782b-4eb7-9518-afd054db5733",
      "cid": "Ikx1Y2FzIFBleXJpbiI",
      "name": "Sticky Note8",
      "type": "n8n-nodes-base.stickyNote",
      "creator": "Lucas Peyrin",
      "position": [
        1472,
        480
      ],
      "typeVersion": 1,
      "notes": "© 2025 Lucas Peyrin"
    },
    {
      "parameters": {
        "assignments": {
          "assignments": [
            {
              "id": "e87952cb-878e-4feb-8261-342eaf887838",
              "name": "summary_string",
              "type": "string",
              "value": "={{ $('String').item.json.json_example_string }}"
            },
            {
              "id": "12345",
              "name": "summary_number",
              "type": "number",
              "value": "={{ $('Number').item.json.json_example_integer }}"
            },
            {
              "id": "67890",
              "name": "summary_boolean",
              "type": "boolean",
              "value": "={{ $('Boolean').item.json.json_example_boolean }}"
            },
            {
              "id": "abcde",
              "name": "summary_null",
              "type": "null",
              "value": "={{ $('Null').item.json.json_example_null }}"
            },
            {
              "id": "fghij",
              "name": "summary_array",
              "type": "array",
              "value": "={{ $('Array').item.json.json_example_array }}"
            },
            {
              "id": "klmno",
              "name": "summary_object",
              "type": "object",
              "value": "={{ $('Object').item.json.json_example_object }}"
            }
          ]
        },
        "options": {}
      },
      "id": "a5d3e674-6e55-4368-9996-6901d5f5d125",
      "cid": "Ikx1Y2FzIFBleXJpbiI",
      "name": "Final Exam",
      "type": "n8n-nodes-base.set",
      "creator": "Lucas Peyrin",
      "position": [
        1952,
        832
      ],
      "typeVersion": 3.4,
      "notes": "© 2025 Lucas Peyrin"
    },
    {
      "parameters": {
        "content": "#### 🎓 期末考试:综合应用\n\n最后这个节点通过表达式从**所有前置节点**获取数据,创建了一个最终对象。\n\n点击此节点,查看每个字段中的表达式。这是对你所学内容的完美总结。\n\n**恭喜!你现在已经掌握了 JSON 的基础知识以及如何在 n8n 中使用它。**\n",
        "height": 516,
        "width": 340,
        "color": 6
      },
      "id": "229b119a-7cdf-4308-b96e-b7e9c269f0ea",
      "cid": "Ikx1Y2FzIFBleXJpbiI",
      "name": "Sticky Note9",
      "type": "n8n-nodes-base.stickyNote",
      "creator": "Lucas Peyrin",
      "position": [
        1840,
        480
      ],
      "typeVersion": 1,
      "notes": "© 2025 Lucas Peyrin"
    }
  ],
  "pinData": {},
  "connections": {
    "Null": {
      "main": [
        [
          {
            "node": "Array",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Array": {
      "main": [
        [
          {
            "node": "Object",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Number": {
      "main": [
        [
          {
            "node": "Boolean",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Object": {
      "main": [
        [
          {
            "node": "Using JSON (Expressions)",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "String": {
      "main": [
        [
          {
            "node": "Number",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Boolean": {
      "main": [
        [
          {
            "node": "Null",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Key & Value": {
      "main": [
        [
          {
            "node": "String",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Execute to Start": {
      "main": [
        [
          {
            "node": "Key & Value",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Using JSON (Expressions)": {
      "main": [
        [
          {
            "node": "Final Exam",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "active": false,
  "settings": {
    "executionOrder": "v1"
  },
  "versionId": "b2988411-008b-47ae-b4bf-1b6a1e584dd2",
  "meta": {
    "instanceId": "54c9e2e31753f3c874da48cdbf9552e23a88fbc2e067ac67b23af8418a890097"
  },
  "id": "R50u6wXiq6c8aAXv",
  "tags": []
}
相关推荐
机器之心4 小时前
扎克伯格看OpenAI直播挖人,北大校友孙之清加入Meta
人工智能·openai
安思派Anspire5 小时前
测试18种RAG技术,找出最优方案(三)
aigc·openai·agent
新智元20 小时前
GPT-4o替代爹味GPT-5!奥特曼光速滑跪,OpenAI连夜回滚「赛博舔狗」
人工智能·openai
新智元20 小时前
马斯克痛失xAI大将!Grok 4缔造者突然离职,长文曝最燃创业内幕
人工智能·openai
机器之心21 小时前
全网最懂图文调研的智能体模型震撼上线,看完我直接卸了浏览器
人工智能·openai
量子位1 天前
被曝蒸馏DeepSeek还造假!欧版OpenAI塌房了
openai·deepseek
货拉拉技术1 天前
AI赋能运营实战:指标扫描自动运营
aigc·openai
得物技术1 天前
AI质量专项报告自动分析生成|得物技术
openai·测试
机器之心1 天前
港大联手月之暗面等开源OpenCUA:人人可造专属电脑智能体
人工智能·openai
哪吒编程1 天前
GPT-5发布引发用户强烈反弹,OpenAI紧急回应
gpt·chatgpt·openai