معالجة البيانات باستخدام Set وCode في n8n | Process Data Using Set and Code in n8n
تعلّم ببساطة، خطوة بخطوة
Learn simply, step by step
معلومة واضحة، ثم خطوة جديدة.
Clear knowledge, one step at a time.
معالجة البيانات باستخدام Set وCode في n8n Process Data Using Set and Code in n8n
ما هي معالجة البيانات في n8n؟
تُعد معالجة البيانات في n8n خطوة مهمة عند بناء Workflows متوسطة أو متقدمة، لأن البيانات القادمة من API أو نموذج أو خدمة خارجية قد لا تكون بالشكل المناسب للخطوة التالية.
يمكنك تنظيم هذه البيانات وتغيير أسماء الحقول أو إنشاء قيم جديدة باستخدام عقد مثل Edit Fields (Set)، واستخدام Code عندما تحتاج إلى معالجة أكثر تخصيصًا.
استخدام Edit Fields (Set) في n8n
تساعدك عقدة Edit Fields (Set) على اختيار البيانات التي تريد الاحتفاظ بها وإضافة حقول جديدة أو تعديل قيم موجودة بدون الحاجة إلى كتابة كود.
على سبيل المثال، إذا أعاد API بيانات كثيرة عن عملة رقمية، بينما تحتاج فقط إلى الاسم والرمز والسعر، يمكنك تجهيز هذه القيم في حقول واضحة قبل إرسالها إلى العقدة التالية.
- name: Bitcoin
- symbol: BTC
- price: 65000
بهذه الطريقة تصبح البيانات أبسط وأسهل في الاستخدام داخل باقي الـWorkflow.
إنشاء قيم جديدة من البيانات
يمكنك أيضًا استخدام Expressions داخل Edit Fields لإنشاء قيمة تعتمد على بيانات عقدة سابقة. على سبيل المثال، يمكنك دمج اسم العملة مع رمزها أو تجهيز رسالة تحتوي على السعر قبل إرسالها إلى خدمة أخرى.
هذا يجعل معالجة البيانات في n8n أكثر مرونة بدون الحاجة إلى كتابة JavaScript في الحالات البسيطة.
متى نستخدم عقدة Code؟
عندما تصبح عملية تعديل البيانات أكثر تعقيدًا، يمكن استخدام عقدة Code. تسمح هذه العقدة بتنفيذ منطق مخصص على البيانات باستخدام الكود.
على سبيل المثال، قد تحتاج إلى إجراء عملية حسابية، أو تعديل مجموعة من العناصر، أو إنشاء قيمة جديدة بناءً على عدة حقول مختلفة.
مثال بسيط باستخدام Code
إذا كانت لديك قيمتان هما السعر والكمية، يمكنك حساب القيمة الإجمالية للصفقة وإضافة النتيجة إلى البيانات قبل تمريرها إلى العقدة التالية.
const price = $json.price;
const quantity = $json.quantity;
return [{
json: {
...$json,
total: price * quantity
}
}];
في هذا المثال يتم الاحتفاظ بالبيانات الأصلية وإضافة حقل جديد باسم total.
Edit Fields أم Code؟
استخدم Edit Fields (Set) عندما تحتاج إلى تنظيم الحقول أو تعديل قيم بسيطة أو إنشاء بيانات جديدة باستخدام Expressions. واستخدم Code عندما تحتاج إلى منطق أو عمليات معالجة لا يمكن تنفيذها بسهولة بالعقد العادية.
نصيحة مهمة
لا تستخدم Code إذا كانت نفس المهمة يمكن تنفيذها بسهولة بعقد n8n العادية. كلما كان الـWorkflow أبسط، أصبح فهمه وصيانته واكتشاف الأخطاء فيه أسهل.
يمكنك معرفة المزيد عن التعامل مع البيانات من خلال
توثيق البيانات الرسمي في n8n.
ويمكنك زيارة
أدوات TheCrypTechAI المجانية
لرؤية أمثلة على أدوات تتعامل مع أنواع مختلفة من البيانات.
الخلاصة
تساعدك معالجة البيانات في n8n على تجهيز المعلومات بالشكل المناسب قبل إرسالها إلى الخطوات التالية. ابدأ بـ Edit Fields للعمليات البسيطة، وانتقل إلى Code فقط عندما تحتاج إلى معالجة أكثر تخصيصًا.
What Is Data Processing in n8n?
Data Processing in n8n is an important part of building intermediate and advanced workflows. Data received from an API, form, or external service may not always be in the format required by the next step.
You can organize this data, rename fields, and create new values using nodes such as Edit Fields (Set). When you need more customized processing, you can use the Code node.
Using Edit Fields (Set) in n8n
The Edit Fields (Set) node allows you to select the data you want to keep, add new fields, and modify existing values without writing code.
For example, an API may return a large amount of information about a cryptocurrency, while your workflow only needs its name, symbol, and price. You can organize these values into clear fields before passing them to the next node.
- name: Bitcoin
- symbol: BTC
- price: 65000
This makes the data cleaner and easier to use throughout the rest of the workflow.
Creating New Values from Existing Data
You can also use Expressions inside Edit Fields to create values based on data from previous nodes. For example, you could combine a cryptocurrency name with its symbol or prepare a message containing its current price before sending it to another service.
This makes Data Processing in n8n flexible without requiring JavaScript for simple transformations.
When Should You Use the Code Node?
When data transformation becomes more complex, you can use the Code node. It allows you to apply custom logic to your workflow data.
For example, you may need to perform a calculation, transform multiple items, or create a new value based on several different fields.
Simple Code Example
If your data contains a price and quantity, you can calculate the total transaction value and add the result before passing the data to the next node.
const price = $json.price;
const quantity = $json.quantity;
return [{
json: {
...$json,
total: price * quantity
}
}];
In this example, the original data is preserved and a new field called total is added.
Edit Fields or Code?
Use Edit Fields (Set) when you need to organize fields, modify simple values, or create new data using Expressions. Use Code when you need custom logic or transformations that cannot be handled easily with standard nodes.
Important Tip
Avoid using Code when the same task can be completed easily with standard n8n nodes. Simpler workflows are usually easier to understand, maintain, and troubleshoot.
You can learn more about working with data in the
official n8n data documentation.
You can also explore
TheCrypTechAI free online tools
to see examples of tools that work with different types of data.
Summary
Data Processing in n8n helps you prepare information in the correct format before sending it to the next workflow steps. Start with Edit Fields for simple transformations and use Code only when you need more customized processing.
اختبر فهمك
Check Your Understanding
سؤالان سريعان لتثبيت أهم ما تعلمته.
Two quick questions to reinforce the key ideas.