Create new serie of data with Jsonata

Hi,

I use a datalogger to make measurment every 10 minutes, but the notecard send my data to notehub every hour or every day (to save power) so my data.qo look like this.

{
    "event": "cc450f26-9dc0-426b-b209-718a9a453755",
    "session": "4f2a3c24-fd59-4cf5-bc50-62b34a8e3de7",
    "best_id": "dev:864475044236916",
    "device": "dev:864475044236916",
    "product": "XXXXXXXXX,
    "received": 1663223136.55936,
    "req": "note.add",
    "when": 1663223124,
    "file": "data.qo",
    "body": {
        "data": {
            "Air_Pressure_(mbar)": [
                {
                    "epoch": 1663221256,
                    "value": "898.06"
                },
                {
                    "epoch": 1663221856,
                    "value": "897.98"
                },
                {
                    "epoch": 1663222456,
                    "value": "897.98"
                },
                {
                    "epoch": 1663223056,
                    "value": "898.03"
                }
            ],
            "Air_Relative_Humidity_(percentage)": [
                {
                    "epoch": 1663221256,
                    "value": "47.60"
                },
                {
                    "epoch": 1663221856,
                    "value": "47.73"

I use this Jsonata expression when I want to modify a data feed, for exemple I put a shift of 45 mbar to air pressure because of a problem on the sensor.

body ~> |data.`Water_Level_(cm)`|{'value': $string($round($number(value)+5, 2))}|

My question is: How to modify the data and create a new data type (for exemple Air_pressure_mod). The goal is to keep the raw data and have also the new modified data.

I send the to iotplotter and it look like that.
https://iotplotter.com/user/feed/289102558818469079
Any advice how to manage this?
Thank you,
Eric

Hi Eric,

You should take a look at using the $merge function in JSONata. This allows you to append data to the original JSON structure, anywhere you like. We have a short guide on using it here:

OK thank you Rob.
I found the solution bellow following your link. Maybe it can help others.

Blockquote

(
    $corrLevel := function($root) {(
    $root ~> |$|{'value': $string($round($number(value)+ 5, 2))}|
    )};

    $Level_to_Flow := function($root) {(
    $root ~> |$|{'value': $string($round($number(value)- 1500, 2))}|
    )};

    {
        "data": $merge([body.data, {"Corr_Water_Level_(cm)": $corrLevel(body.data.`Water_Level_(cm)`),"Flow_(L/min)": $Level_to_Flow(body.data.`Water_Level_(cm)`) }])
    }
)
1 Like