| name | n8n-5-data-transformation-with-code-node |
| description | Sub-skill of n8n: 5. Data Transformation with Code Node (+1). |
| version | 1.0.0 |
| category | operations |
| type | reference |
| scripts_exempt | true |
5. Data Transformation with Code Node (+1)
5. Data Transformation with Code Node
const items = $input.all();
const groupedByCategory = items.reduce((acc, item) => {
const category = item.json.category || 'uncategorized';
if (!acc[category]) {
acc[category] = [];
}
acc[category].push(item.json);
return acc;
}, {});
const categoryStats = Object.entries(groupedByCategory).map(([category, items]) => {
const values = items.map(i => parseFloat(i.value) || 0);
return {
category,
count: items.length,
total: values.reduce((a, b) => a + b, 0),
average: values.length > 0 ? values.reduce((a, b) => a + b, 0) / values.length : 0,
min: Math.min(...values),
max: Math.max(...values),
items: items
};
});
categoryStats.sort((a, b) => b.total - a.total);
const result = {
generated_at: new Date().toISOString(),
total_items: items.length,
total_categories: categoryStats.length,
categories: categoryStats
};
return result;
const maxRetries = 3;
const baseDelay = 1000;
async function fetchWithRetry(url, options, attempt = 1) {
try {
const response = await fetch(url, options);
if (!response.ok) {
throw new Error(`HTTP ${response.status}: ${response.statusText}`);
}
return await response.json();
} catch (error) {
if (attempt >= maxRetries) {
throw error;
}
const delay = baseDelay * Math.pow(2, attempt - 1);
await new Promise(resolve => setTimeout(resolve, delay));
return fetchWithRetry(url, options, attempt + 1);
}
}
const item = $input.item.json;
try {
const result = await fetchWithRetry(
,
{
: ,
: {
: ,
:
},
: .(item)
}
);
{
...item,
: result,
:
};
} (error) {
{
...item,
: error.,
:
};
}
6. Credentials Management
export N8N_ENCRYPTION_KEY="your-32-char-encryption-key-here"
export N8N_USER_MANAGEMENT_JWT_SECRET="your-jwt-secret"
export DB_TYPE=postgresdb
export DB_POSTGRESDB_HOST=localhost
export DB_POSTGRESDB_PORT=5432
export DB_POSTGRESDB_DATABASE=n8n
export DB_POSTGRESDB_USER=n8n
export DB_POSTGRESDB_PASSWORD=secure_password
{
"name": "Using Credentials Securely",
"nodes": [
{
"parameters": {
"url": "={{ $env.API_BASE_URL }}/users",
"authentication": "predefinedCredentialType",
"nodeCredentialType": "httpHeaderAuth",
"sendHeaders": true,
"headerParameters": {
"parameters": [
{
"name": "X-Custom-Header",
"value": "={{ $env.CUSTOM_HEADER_VALUE }}"
}
]
}
},
"id": "http",
"name": "API Call"