# Transformation Function

### Overview

A **Transformation Function** in Qubitro allows you to **modify, rename, and structure incoming data** before it is stored or used for automation.

### How Transformation Functions Work

{% hint style="warning" %}
Transformation Functions run **after Decoder Functions** in a system-level order and cannot be changed.
{% endhint %}

{% @mermaid/diagram content="graph LR;
A\[Device Publishes Data] --> B{Decoder Function Exists?};

B -- Yes --> C\[Apply Decoder Function];
B -- No --> D\[Use Raw Data];

C --> E\[Apply Transformation Function];
D --> E;

E --> F\[Store Transformed Data in Qubitro];

%% Style Adjustments
style A fill:#d9d9d9,stroke:#000,color:#000;
style B fill:#f2f2f2,stroke:#000,color:#000;
style E fill:#e0e0e0,stroke:#000,color:#000;" %}

### JSONata for Data Transformation

Transformation Functions in Qubitro use [**JSONata**](https://jsonata.org/), a lightweight query and transformation language designed for JSON data.

{% hint style="info" %}
JSONata is optimized for real-time data processing, allowing flexible modifications without complex scripting.
{% endhint %}

<mark style="background-color:yellow;">For examples and best practices, check out our guidance.</mark>

{% embed url="<https://qubitro.gitbook.io/qubitro-documentation/guides/functions-and-data-processing/how-to-use-jsonata-for-real-time-data-transformation-in-qubitro>" %}

### Creating a Transformation Function

Transformation functions can be created from the **Functions tab** in the **device details page**:

{% stepper %}
{% step %}
**Open the Functions Tab**

1. Navigate to the **device details page** in the **Qubitro Portal**.
2. Click the **Functions** tab.
3. Click **Create function** and select **Transformation Function**.
4. Choose **JSONata** as the function type.
   {% endstep %}

{% step %}
**Write the JSONata Expression**

1. Enter your **JSONata transformation logic** in the editor.
2. Modify, restructure, or filter data as needed.
3. Click **Test** to validate the transformation output.

<figure><img src="https://3109056034-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FfQfo4zX1x0hfOimS9E2u%2Fuploads%2FUEEOcEc21OzTpo4eqFxh%2FCleanShot%202025-03-01%20at%2022.44.25%402x.png?alt=media&#x26;token=65c60887-2194-44d0-97f8-67aba6baa65a" alt=""><figcaption></figcaption></figure>
{% endstep %}

{% step %}
**Save and Apply the Function**

1. Click **Save and complete** to activate the function.
2. The function will now **automatically process all incoming data** for this device.

{% hint style="success" %}
Transformation functions can be activated during creation or manually enabled/disabled under the **Configuration** page in the **Device Settings**.
{% endhint %}
{% endstep %}
{% endstepper %}
