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:
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.
JSONata Expression:
{
"sensor_type": sensor.type,
"current_temperature": sensor.readings.current,
"average_temperature": sensor.readings.average
}
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.
JSONata Expression:
{
"temperature_value": sensors[type='temperature'].value,
"humidity_value": sensors[type='humidity'].value
}
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.
JSONata Expression:
{
"sum": $sum(values),
"average": $average(values),
"max": $max(values),
"min": $min(values)
}
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.
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"
}
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.
Last updated