How to Use JSONata for Real-Time Data Transformation in Qubitro

This guide explains how to use JSONata expressions in Qubitro Transformation Functions to manipulate and structure real-time data before it is stored or used for automated actions.

JSONata provides a flexible, lightweight way to filter, calculate, and restructure data without writing traditional code.

A few things to remember:

Data can be modified on-the-fly before storage and automation.

TF applies immediately to incoming data, whether raw or already decoded.

How Transformation Functions Work in Qubitro

Once a device sends data, TF automatically applies the JSONata expression configured by the user.

If a Decoder Function exists, it will process raw data first, and then TF will apply JSONata expressions on the decoded output. If there is no decoder, TF will apply transformations directly to the published data.

Basic JSONata Syntax

Here are some core JSONata concepts:

  • Mathematical functions: $sum(values), $average(values), $max(values), $min(values).

  • Ternary operators: condition ? trueValue : falseValue.

  • String and numeric literals: "text" for strings, 42 for numbers.

  • Using the root object: $ refers to the root JSON object.

  • Accessing fields: fieldname retrieves a specific property.

Practical Implementations

Example 1: Flattening Nested JSON

Convert a multi-level JSON object into a flat structure for better Qubitro compatibility.

1

Incoming Data:

{
  "sensor": {
    "type": "temperature",
    "readings": {
      "current": 22,
      "average": 21
    }
  }
}
2

JSONata Expression:

{
  "sensor_type": sensor.type,
  "current_temperature": sensor.readings.current,
  "average_temperature": sensor.readings.average
}
3

Transformed Data:

{
  "sensor_type": "temperature",
  "current_temperature": 22,
  "average_temperature": 21
}

Example 2: Extracting Values from Arrays

Transform an array of objects into structured key-value pairs.

1

Incoming Data:

{
  "sensors": [
    {"type": "temperature", "value": 22},
    {"type": "humidity", "value": 55}
  ]
}
2

JSONata Expression:

{
  "temperature_value": sensors[type='temperature'].value,
  "humidity_value": sensors[type='humidity'].value
}
3

Transformed Data:

{
  "temperature_value": 22,
  "humidity_value": 55
}

Example 3: Performing Mathematical Calculations

Use JSONata to compute sums, averages, and find min/max values dynamically.

1

Incoming Data:

{
  "values": [5, 10, 15, 20, 25]
}
2

JSONata Expression:

{
  "sum": $sum(values),
  "average": $average(values),
  "max": $max(values),
  "min": $min(values)
}
3

Transformed Data:

{
  "sum": 75,
  "average": 15,
  "max": 25,
  "min": 5
}

Example 4: Time Formatting and Duration Calculation

Modify timestamps into human-readable formats or compute durations.

1

Incoming Data:

{
  "event": {
    "start_time": "2020-01-01T08:00:00Z",
    "end_time": "2020-01-01T12:00:00Z"
  }
}
2

JSONata Expression:

{
  "start_date": $fromMillis($toMillis(event.start_time), '[Y0001]-[M01]-[D01]'),
  "end_date": $fromMillis($toMillis(event.end_time), '[Y0001]-[M01]-[D01]'),
  "duration_hours": ($toMillis(event.end_time) - $toMillis(event.start_time)) / (1000*60*60) & " hours"
}
3

Transformed Data:

{
  "start_date": "2020-01-01",
  "end_date": "2020-01-01",
  "duration_hours": "4 hours"
}

Example 5: Dynamically Accessing Data Using $lookup()

This method is useful when field names may change dynamically.

1

Incoming Data:

{
  "sensorData": {
    "temperature": 22,
    "humidity": 78
  }
}
2

JSONata Expression:

{
  "temp_value": $lookup(sensorData, 'temperature'),
  "humidity_value": $lookup(sensorData, 'humidity')
}
3

Transformed Data:

{
  "temp_value": 22,
  "humidity_value": 78
}

Last updated