Transform JSON with Custom Scripts
Use the Transform Builder's custom script operator to handle complex transformation logic that goes beyond built-in operations.
Last updated:
Sometimes your transformation needs go beyond simple renaming or type casting. The Transform Builder includes a powerful Custom Script operator that lets you write JavaScript to handle any edge case.
๐ฉ The Scenario
You have an API response where permissions is a comma-separated string, and you need to convert it to a proper array:
{
"userId": 42,
"permissions": "read,write,admin",
"metadata": "{\"theme\":\"dark\",\"lang\":\"en\"}"
}
You need:
{
"userId": 42,
"permissions": ["read", "write", "admin"],
"metadata": {"theme": "dark", "lang": "en"}
}
๐ ๏ธ Step-by-Step
- Open the Transform Builder.
- Load your JSON data.
- Use Magic Map to auto-detect all fields, or drag fields manually.
- For the
permissionsfield, click the โก icon to add an operation. - Select Custom Script from the operations list.
- Enter your transformation logic:
// The current field value is available as `value`
// The entire source record is available as `record`
return value.split(',').map(p => p.trim());
- For the
metadatafield, add another Custom Script:
return JSON.parse(value);
๐ Security Sandbox
Custom scripts run in a sandboxed environment. The following globals are blocked for security:
window,document,fetch,XMLHttpRequestlocalStorage,sessionStorage,indexedDBprocess,require,Worker
This ensures that transformation logic cannot make network requests or access browser storage.
๐ก Available Variables
| Variable | Description |
|---|---|
value | The current value of the field being transformed |
record | The entire source record (root object) |
๐งฎ More Examples
Extract domain from email:
return value.split('@')[1];
Calculate age from birthdate:
const birth = new Date(value);
const today = new Date();
return Math.floor((today - birth) / (365.25 * 24 * 60 * 60 * 1000));
Conditional complex logic:
if (record.country === 'US') {
return value.replace(/(\d{3})(\d{3})(\d{4})/, '($1) $2-$3');
}
return value;
โ๏ธ Chaining with Other Operations
Custom scripts can be combined with built-in operations. For example:
- Trim โ removes whitespace
- Custom Script โ splits into array
- The operations execute in order, left to right.
๐ Try it now
Open the Transform Builder and add a Custom Script operation to any field mapping.