Basic Operations

Simple field assignment, string operations, numeric calculations, and boolean operations using the Code Processor.

Simple Field Assignment

Create new attributes with literal values or simple operations:

// Assign literal values
item['attributes']['test_field'] = 'hello';
item['attributes']['score'] = 100 * 2;
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
item['attributes']['full_name'] = item['attributes']['first'] + ' ' + item['attributes']['last'];

// String methods
item['attributes']['uppercase_name'] = item['attributes']['name'].toUpperCase();

// Template literals (ES6)
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
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
item['attributes']['label'] = (item['attributes']['service'] || 'unknown') + '::' + item['attributes']['id'];
// Result: "unknown::123"

Numeric Operations

Perform calculations and use Math functions:

// Basic arithmetic
item['attributes']['doubled_value'] = item['attributes']['value'] * 2;

// Math functions
item['attributes']['calc_result'] = Math.round(item['attributes']['score'] / 3);
item['attributes']['random_value'] = Math.floor(Math.random() * 100);

Boolean Operations

Use boolean literals, comparison operators, and logical operators:

// Boolean literals
item['attributes']['is_valid'] = true;
item['attributes']['not_valid'] = false;

// Comparison operators
item['attributes']['is_high_score'] = item['attributes']['score'] > 80;
item['attributes']['is_critical'] = item['attributes']['level'] === 'ERROR';

// Logical operators
item['attributes']['combined'] = item['attributes']['is_valid'] && item['attributes']['is_active'];
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
}

Accessing Field Paths

When using log_parsing_mode: full, incoming JSON is parsed into the body field. You can access body fields directly in the Code Processor:

// Access body fields directly
item['attributes']['user_id'] = item['body']['user']['id'];
item['attributes']['score_doubled'] = item['body']['metrics']['score'] * 2;

Alternatively, extract fields using OTTL set statements first, then use the Code Processor:

// After extracting with: set(attributes["score"], body["metrics"]["score"])
item['attributes']['score_doubled'] = item['attributes']['score'] * 2;