Skip to main content

JS Code

Executes custom JavaScript code to transform items.

Overview

The JS Code node runs JavaScript for each item, giving you full programming control over transformations. Use it for:

  • Complex calculations
  • Custom data parsing
  • Conditional logic
  • String manipulation
  • Data validation

Parameters

ParameterDescriptionRequired
JS CodeJavaScript code to executeYes

Code

Write JavaScript that processes the item. The code has access to:

  • item - The current input item

Return the transformed item, a new object, or an array of items.

Output

The return value of your code becomes the output. If you return an array, each element becomes a separate output item.

Input:

{ "firstName": "John", "lastName": "Doe", "price": "19.99" }

Code:

return {
fullName: item.firstName + ' ' + item.lastName,
price: parseFloat(item.price),
formattedPrice: '$' + parseFloat(item.price).toFixed(2)
};

Output:

{
"fullName": "John Doe",
"price": 19.99,
"formattedPrice": "$19.99"
}

Examples

Parse JSON String

const data = JSON.parse(item.jsonString);
return {
...item,
parsedData: data
};

Conditional Transformation

const status = item.amount > 1000 ? 'high' : 'normal';
const priority = item.urgent ? 1 : 2;

return {
...item,
status,
priority
};

String Manipulation

return {
...item,
email: item.email.toLowerCase().trim(),
slug: item.title.toLowerCase().replace(/\s+/g, '-'),
initials: item.firstName[0] + item.lastName[0]
};

Date Handling

const date = new Date(item.timestamp);
return {
...item,
year: date.getFullYear(),
month: date.getMonth() + 1,
day: date.getDate(),
formatted: date.toISOString().split('T')[0]
};

Array Processing

const tags = item.tagString.split(',').map(t => t.trim());
const uniqueTags = [...new Set(tags)];

return {
...item,
tags: uniqueTags,
tagCount: uniqueTags.length
};

Validation

const errors = [];

if (!item.email || !item.email.includes('@')) {
errors.push('Invalid email');
}

if (!item.name || item.name.length < 2) {
errors.push('Name too short');
}

return {
...item,
isValid: errors.length === 0,
validationErrors: errors
};

Return Multiple Items

// Split one item into multiple
return [
{ type: 'header', data: item.header },
{ type: 'body', data: item.body },
{ type: 'footer', data: item.footer }
];

Object Restructuring

return {
customer: {
name: item.customerName,
email: item.customerEmail
},
order: {
id: item.orderId,
total: item.orderTotal
}
};

Available Context

VariableDescription
itemCurrent input item object

Tips

  • Always return an object or array (the output item(s))
  • Use ...item spread to preserve existing fields
  • Code runs in an isolated JavaScript VM
  • Errors in code create error items
  • Return an array to output multiple items from one input
  • Test with sample data before running on full dataset
  • Complex logic is fine, but keep it readable