Complex Transformations

Conditional logic, nested objects, array operations, and multi-step transformations with edx_code.

Conditional Logic

Use ternary operators, if/else statements, and boolean comparisons:

// Ternary operator
edx_code("item['attributes']['level_category'] = item['attributes']['score'] > 50 ? 'high' : 'low';")

// Boolean equality check
edx_code("item['attributes']['is_critical'] = item['attributes']['level'] === 'ERROR';")

// Complex if/else with nested object access
edx_code("{
  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
edx_code("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
edx_code("if (!item['attributes']['user']) item['attributes']['user'] = {};")

// Set nested properties
edx_code("item['attributes']['user']['role'] = 'admin';")
edx_code("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)
edx_code("item['attributes']['values'] = [1, 2, 3, 4, 5];")
edx_code("item['attributes']['tags'] = ['tag1', 'tag2', 'tag3'];")

// Array length property
edx_code("item['attributes']['tag_count'] = item['attributes']['tags'].length;")

// Array.map() - transform each element
edx_code("item['attributes']['doubled'] = item['attributes']['values'].map(v => v * 2);")

// Array.filter() - select elements matching condition
edx_code("item['attributes']['evens'] = item['attributes']['values'].filter(v => v % 2 === 0);")

// Array.reduce() - aggregate values (requires initial value)
edx_code("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
}

Multiple Operations in One Statement

Execute multiple JavaScript statements using block syntax:

edx_code("{
  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
edx_code("item['attributes']['json_string'] = JSON.stringify({key: 'value', num: 123});")

// JSON.parse (if you have a JSON string)
edx_code("item['attributes']['parsed'] = JSON.parse(item['attributes']['json_string']);")

Date and Time Operations

Work with Date objects and generate timestamps:

// Current timestamp
edx_code("item['attributes']['current_time'] = new Date().toISOString();")

// Timestamp in milliseconds
edx_code("item['attributes']['timestamp_ms'] = Date.now();")

// Date calculations
edx_code("{
  const date = new Date();
  date.setDate(date.getDate() + 7);
  item['attributes']['next_week'] = date.toISOString();
}")