Skip to content

Basic Syntax and Field Access

Overview

The expression language provides a powerful and intuitive way to access and manipulate data fields within structured data. This chapter covers the fundamental syntax elements and field access mechanisms.

Basic Field Access

Simple Field References

The most basic form of field access is a direct reference to a field name:

js
category
user_id
timestamp

Field names can contain:

  • Letters (a-z, A-Z)
  • Numbers (0-9)
  • Underscores (_)
  • Must start with a letter or underscore

Dot Notation

For accessing nested fields, dot notation is used to traverse the object hierarchy:

js
user.name
address.city.postal_code
metadata.created_at

Each dot represents a level of nesting in the data structure. The expression evaluates each component from left to right, accessing deeper levels of the structure.

Array Access

Basic Array Indexing

Arrays can be accessed using square bracket notation with zero-based indexing:

js
items[0]           // First element
messages[5]        // Sixth element
data.points[3]     // Fourth element in a nested array

Combining Dot Notation with Array Access

Array access can be combined with dot notation for complex data structures:

js
users[0].name              // Name of first user
props.arr[0].foo          // Nested property access after array indexing
data.points[3].coordinates.x   // Accessing nested fields after array index

Important Array Access Rules

  1. Array indices must be non-negative integers
  2. Accessing an index beyond array bounds will result in an error
  3. Array access can be used at any point in a field path
  4. Multiple array accesses can be combined in a single expression

Type System Interaction

Field Value Types

Fields can contain different types of values:

  1. Strings
  2. Numbers (integers or floating-point)
  3. Booleans
  4. Arrays
  5. Nested objects

Type-Aware Access

The expression language is type-aware and enforces type safety:

js
// Valid expressions
user.age > 18            // Numeric comparison
user.name == "John"      // String comparison
user.active == true      // Boolean comparison

// Invalid expressions (will cause type errors)
user.age == "18"         // Comparing number with string
user.name > true         // Invalid comparison between string and boolean

Error Handling

Common Field Access Errors

  1. Missing Field Error

    js
    nonexistent_field        // Error: field 'nonexistent_field' not found
    user.unknown_property    // Error: field 'unknown_property' not found in user
  2. Invalid Array Access

    js
    items[-1]               // Error: negative array index
    items[999999]          // Error: array index out of bounds
  3. Type Mismatch

    js
    // Assuming 'data' is a string
    data[0]                // Error: array access on non-array type
  4. Null/Undefined Handling

    js
    // Assuming 'user' is null
    user.name              // Error: cannot access property of null

Best Practices

  1. Field Name Conventions

    • Use descriptive, meaningful field names
    • Follow consistent naming conventions
    • Prefer snake_case or camelCase based on your data structure
  2. Nested Access

    • Keep nesting levels reasonable (avoid deep nesting)
    • Consider breaking up deeply nested expressions
    • Use intermediate variables when appropriate
  3. Array Access

    • Validate array bounds before access
    • Consider using functions for array operations
    • Be cautious with hardcoded array indices
  4. Type Safety

    • Be aware of field types
    • Use appropriate operators for each type
    • Handle type conversion explicitly when needed

Complex Nested Access

js
// Multiple levels of nesting
organization.departments[0].employees[5].contact.email

// Mixed array and object access
data.series[0].measurements[3].sensor.readings[0].value