Basic Operations
2 minute read
Simple Field Assignment
Create new attributes with literal values or simple operations:
edx_code("item['attributes']['test_field'] = 'hello';")
edx_code("item['attributes']['score'] = 100 * 2;")
edx_code("item['attributes']['is_valid'] = true;")
Input:
{
"timestamp": "2025-09-28T10:30:45.123Z",
"message": "Test log entry"
}
Output:
{
"timestamp": "2025-09-28T10:30:45.123Z",
"message": "Test log entry",
"test_field": "hello",
"score": 200,
"is_valid": true
}
String Operations
Concatenate strings and use JavaScript string methods:
// String concatenation
edx_code("item['attributes']['full_name'] = item['attributes']['first'] + ' ' + item['attributes']['last'];")
// String methods
edx_code("item['attributes']['uppercase_name'] = item['attributes']['name'].toUpperCase();")
// Template literals (ES6)
edx_code("item['attributes']['summary'] = `User ${item['attributes']['name']} scored ${item['attributes']['score']}`;")
Input:
{
"first": "John",
"last": "Doe",
"name": "testuser",
"score": 95
}
Output:
{
"first": "John",
"last": "Doe",
"name": "testuser",
"score": 95,
"full_name": "John Doe",
"uppercase_name": "TESTUSER",
"summary": "User testuser scored 95"
}
Handling Undefined Fields
When concatenating or using template literals with undefined fields, JavaScript inserts the string "undefined"
rather than causing an error:
// If 'service' field doesn't exist
edx_code("item['attributes']['label'] = `${item['attributes']['service']}::${item['attributes']['id']}`;")
// Result: "undefined::123"
Best practice - Use null-safe operators to provide defaults:
// Provide a default value for missing fields
edx_code("item['attributes']['label'] = (item['attributes']['service'] || 'unknown') + '::' + item['attributes']['id'];")
// Result: "unknown::123"
Numeric Operations
Perform calculations and use Math functions:
// Basic arithmetic
edx_code("item['attributes']['doubled_value'] = item['attributes']['value'] * 2;")
// Math functions
edx_code("item['attributes']['calc_result'] = Math.round(item['attributes']['score'] / 3);")
edx_code("item['attributes']['random_value'] = Math.floor(Math.random() * 100);")
Boolean Operations
Use boolean literals, comparison operators, and logical operators:
// Boolean literals
edx_code("item['attributes']['is_valid'] = true;")
edx_code("item['attributes']['not_valid'] = false;")
// Comparison operators
edx_code("item['attributes']['is_high_score'] = item['attributes']['score'] > 80;")
edx_code("item['attributes']['is_critical'] = item['attributes']['level'] === 'ERROR';")
// Logical operators
edx_code("item['attributes']['combined'] = item['attributes']['is_valid'] && item['attributes']['is_active'];")
edx_code("item['attributes']['either'] = item['attributes']['is_critical'] || item['attributes']['is_high_score'];")
Input:
{
"score": 85,
"level": "INFO",
"status": "active"
}
Output:
{
"score": 85,
"level": "INFO",
"status": "active",
"is_valid": true,
"not_valid": false,
"is_high_score": true,
"is_critical": false,
"is_active": true,
"combined": true,
"either": true
}
Note
Boolean values are preserved as JSON booleans in the output, not converted to strings. Use strict equality (===
) for type-safe comparisons.Accessing Field Paths
When using log_parsing_mode: full
, incoming JSON is parsed into the body
field. Use OTTL set
statements to extract body fields to attributes before using edx_code
:
// Extract from body first
set(attributes["user_id"], body["user"]["id"])
set(attributes["score"], body["metrics"]["score"])
// Then use edx_code on attributes
edx_code("item['attributes']['score_doubled'] = item['attributes']['score'] * 2;")
Alternatively, access body fields directly in edx_code
:
edx_code("item['attributes']['user_id'] = item['body']['user']['id'];")