Complex Transformations
3 minute read
Code Processor Format
All examples on this page show JavaScript code as you would write it in the Code Processor editor. Edge Delta automatically converts this to the proper OTTL format behind the scenes.Conditional Logic
Use ternary operators, if/else statements, and boolean comparisons:
// Ternary operator for simple conditions
item['attributes']['level_category'] = item['attributes']['score'] > 50 ? 'high' : 'low';
// Boolean equality check
item['attributes']['is_critical'] = item['attributes']['level'] === 'ERROR';
// Complex if/else with nested object access
if (item['attributes']['metadata'] && item['attributes']['metadata']['environment'] === 'testing') {
item['attributes']['env_label'] = 'test-env';
} else {
item['attributes']['env_label'] = 'other';
}
// Modulo-based A/B flag
item['attributes']['ab_flag'] = item['attributes']['test_id'] % 2 === 0 ? 'A' : 'B';
Input:
{
"score": 75,
"level": "ERROR",
"metadata": {
"environment": "testing"
},
"test_id": 42
}
Output:
{
"score": 75,
"level": "ERROR",
"level_category": "high",
"is_critical": true,
"metadata": {
"environment": "testing"
},
"env_label": "test-env",
"test_id": 42,
"ab_flag": "A"
}
Nested Object Manipulation
Create and modify nested objects with null-safe checking:
// Initialize nested object if it doesn't exist
if (!item['attributes']['user']) {
item['attributes']['user'] = {};
}
// Set nested properties
item['attributes']['user']['role'] = 'admin';
item['attributes']['user']['status'] = 'active';
Output:
{
"user": {
"role": "admin",
"status": "active"
}
}
Array Operations
Create arrays, access elements, and use array methods with ES6 arrow functions:
// Create arrays (numeric and string)
item['attributes']['values'] = [1, 2, 3, 4, 5];
item['attributes']['tags'] = ['tag1', 'tag2', 'tag3'];
// Array length property
item['attributes']['tag_count'] = item['attributes']['tags'].length;
// Array.map() - transform each element
item['attributes']['doubled'] = item['attributes']['values'].map(v => v * 2);
// Array.filter() - select elements matching condition
item['attributes']['evens'] = item['attributes']['values'].filter(v => v % 2 === 0);
// Array.reduce() - aggregate values (requires initial value)
item['attributes']['sum'] = item['attributes']['values'].reduce((a, b) => a + b, 0);
Input:
{
"message": "Test log entry"
}
Output:
{
"message": "Test log entry",
"values": [1, 2, 3, 4, 5],
"tags": ["tag1", "tag2", "tag3"],
"tag_count": 3,
"doubled": [2, 4, 6, 8, 10],
"evens": [2, 4],
"sum": 15
}
Note
Arrays are preserved as JSON arrays in the output. ES6 arrow functions (=>) are supported in array methods. Always provide an initial value to reduce() for safety.Multiple Operations in One Statement
Execute multiple JavaScript statements in sequence:
// Use const or let for intermediate calculations
const base = item['attributes']['value'];
item['attributes']['a'] = base;
item['attributes']['b'] = base * 2;
item['attributes']['c'] = item['attributes']['a'] + item['attributes']['b'];
item['attributes']['summary'] = `a=${item['attributes']['a']},b=${item['attributes']['b']},c=${item['attributes']['c']}`;
Input:
{
"value": 10
}
Output:
{
"value": 10,
"a": 10,
"b": 20,
"c": 30,
"summary": "a=10,b=20,c=30"
}
Variables declared with const or let within a block are scoped to that block and can be referenced by subsequent statements within the same edx_code call.
JSON Operations
Parse and stringify JSON data:
// JSON.stringify - convert object to string
item['attributes']['json_string'] = JSON.stringify({key: 'value', num: 123});
// JSON.parse - parse JSON string to object
item['attributes']['parsed'] = JSON.parse(item['attributes']['json_string']);
Date and Time Operations
Work with Date objects and generate timestamps:
// Current timestamp in ISO format
item['attributes']['current_time'] = new Date().toISOString();
// Timestamp in milliseconds since epoch
item['attributes']['timestamp_ms'] = Date.now();
// Date calculations - add 7 days
const date = new Date();
date.setDate(date.getDate() + 7);
item['attributes']['next_week'] = date.toISOString();