Prepare array of JSON-Objects with jsonata through change-node for node-red-dashboard multiline-chart

Hi,
trying to transform an array of JSON-objects for dashboard-chart-node, I have problems with jsonata-code.
Probably it's not hard to solve, thank you for helping!

The third graph ('CALC') is already prepared properly, the two others ('DATEN.humidy' /'DATEN.temperature') don't work.

Underneath input-/output-msg.payload and the jsonata-code for the change-node. The problematic part is written bold, with a comment at the end of the line.

Input msg.payload:

[
  {
    "DATUM": 1640024797000,
    "DATEN": {
      "temperature": 14.28,
      "humidity": 58
    },
    "CALC": 29
  },
  {
    "DATUM": 1640024787000,
    "DATEN": {
      "temperature": 14.33,
      "humidity": 59
    },
    "CALC": 29
  },
  {
    "DATUM": 1640023606000,
    "DATEN": {
      "temperature": 14.24,
      "humidity": 62
    },
    "CALC": 30
  }
]

change node:

set msg.payload to jsonata:

(
  $series := [
    { "field_Y": **"DATEN.humidity"**,  "field_X": "DATUM", "label": "relative Luftfeuchtigkeit in %", "header": "topic"},  /* "DATEN.temperature" is not correct, how could it be written to properly display the temperature?*/
    { "field_Y": **"DATEN"**,  "field_X": "DATUM", "label": "Temperatur in °C", "header": "topic"},
    { "field_Y": "CALC",    "field_X": "DATUM", "label": "Enthalpie in kj/kg Luft", "header": "topic"}
  ];

  [
    {
      "series": $series.label,
      "data": $series.[
        (
          $yaxis := $.field_Y;
          $xaxis := $.field_X;
          $$.payload.{
            "x": $lookup($, $xaxis),
            "y": $lookup($, $yaxis)
          }
        )
      ]
    }
  ]
)

payload output:

[
  {
    "series": [
      "relative Luftfeuchtigkeit in %",
      "Temperatur in °C",
      "Enthalpie in kj/kg Luft"
    ],
    "data": [
      [
        {
          "x": 1640024797000
        },
        {
          "x": 1640024787000
        },
        {
          "x": 1640023606000
        }
      ],
      [
        {
          "x": 1640024797000,
          "y": {
            "temperature": 14.28,
            "humidity": 58
          }
        },
        {
          "x": 1640024787000,
          "y": {
            "temperature": 14.33,
            "humidity": 59
          }
        },
        {
          "x": 1640023606000,
          "y": {
            "temperature": 14.24,
            "humidity": 62
          }
        }
      ],
      [
        {
          "x": 1640024797000,
          "y": 29
        },
        {
          "x": 1640024787000,
          "y": 29
        },
        {
          "x": 1640023606000,
          "y": 30
        }
      ]
    ]
  }
]

The $eval() function should work.

"y": $eval("$." & $yaxis)

To expand on the previous answer -- the $lookup function will only work for 1 level, which is why DATEN.xxx cannot be used directly.

On the other hand, $eval(...) can take any expression, including the dotted notation you need:

  [
    {
      "series": $series.label,
      "data": $series@$s.[
        $$.payload.{
          "x": $eval($s.field_X),
          "y": $eval($s.field_Y)
        }
      ]
    }
  ]

FYI - you can also use the Context Variable binding syntax @$var to assign each series definition to a variable name, to avoid the intermediate variables for $xaxis and $yaxis.

Works perfectly, thank you!

(
  $series := [
    { "field_Y": "DATEN.humidity",  "field_X": "DATUM", "label": "relative Luftfeuchtigkeit in %", "header": "topic"},
    { "field_Y": "DATEN.temperature",  "field_X": "DATUM", "label": "Temperatur in °C", "header": "topic"},
    { "field_Y": "CALC",    "field_X": "DATUM", "label": "Enthalpie in kj/kg Luft", "header": "topic"}
  ];

  [
    {
      "series": $series.label,
      "data": $series.[
        (
          $yaxis := $.field_Y;
          $xaxis := $.field_X;
          $$.payload.{
            "x": $eval("$." & $xaxis),
            "y": $eval("$." & $yaxis)
          }
        )
      ]
    }
  ]
)

It would be nice to shorten the code, but if I copy that code into mine the result is just one array with the objects of the three graphs.

output msg.payload:

{
  "series": [
    "relative Luftfeuchtigkeit in %",
    "Temperatur in °C",
    "Enthalpie in kj/kg Luft"
  ],
  "data": [
    {
      "x": 1640075700000,
      "y": 60
    },
    {
      "x": 1640075670000,
      "y": 60
    },
    {
      "x": 1640075660000,
      "y": 60
    },
    {
      "x": 1640075700000,
      "y": 10.1
    },
    {
      "x": 1640075670000,
      "y": 9.99
    },
    {
      "x": 1640075660000,
      "y": 9.99
    },
    {
      "x": 1640075700000,
      "y": 22
    },
    {
      "x": 1640075670000,
      "y": 21
    },
    {
      "x": 1640075660000,
      "y": 21
    }
  ]
}

Yep, looks like I dropped one level of square brackets...

The shortened version works perfectly now, thank you!

jsonata-code for change-node:

(
  $series := [
    { "field_Y": "DATEN.humidity",  "field_X": "DATUM", "label": "relative Luftfeuchtigkeit in %", "header": "topic"},
    { "field_Y": "DATEN.temperature",  "field_X": "DATUM", "label": "Temperatur in °C", "header": "topic"},
    { "field_Y": "CALC",    "field_X": "DATUM", "label": "Enthalpie in kJ/kg Luft", "header": "topic"}
  ];

  [
    {
      "series": $series.label,
      "data": $series@$s.[[
        $$.payload.{
          "x": $eval($s.field_X),
          "y": $eval($s.field_Y)
        }
      ]]
    }
  ]
)

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.