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

**URL:** <https://discourse.nodered.org/t/prepare-array-of-json-objects-with-jsonata-through-change-node-for-node-red-dashboard-multiline-chart/55495>\
**Category:** General\
**Tags:** node-red-dashboard, chart\
**Created:** [20 December 2021 21:55 UTC](https://discourse.nodered.org/t/prepare-array-of-json-objects-with-jsonata-through-change-node-for-node-red-dashboard-multiline-chart/55495 "2021-12-20T21:55:45Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![hwr](https://avatars.discourse-cdn.com/v4/letter/h/c0e974/32.png) [@hwr](https://discourse.nodered.org/u/hwr)\
**Post date:** [20 December 2021 21:55 UTC](https://discourse.nodered.org/t/prepare-array-of-json-objects-with-jsonata-through-change-node-for-node-red-dashboard-multiline-chart/55495/1 "2021-12-20T21:55:45Z")

</div>

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:

```auto
[
  {
    "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:

```auto
(
  $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:

```auto
[
  {
    "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
        }
      ]
    ]
  }
]

```

---

<div class="post-metadata">

**Author:** ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)\
**Post date:** [21 December 2021 01:01 UTC](https://discourse.nodered.org/t/prepare-array-of-json-objects-with-jsonata-through-change-node-for-node-red-dashboard-multiline-chart/55495/2 "2021-12-21T01:01:47Z")

</div>

The $eval() function should work.

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

```

---

<div class="post-metadata">

**Author:** ![shrickus](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/shrickus/32/517_2.png) [@shrickus](https://discourse.nodered.org/u/shrickus)\
**Post date:** [21 December 2021 03:22 UTC](https://discourse.nodered.org/t/prepare-array-of-json-objects-with-jsonata-through-change-node-for-node-red-dashboard-multiline-chart/55495/3 "2021-12-21T03:22:27Z")

</div>

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:

```auto
  [
    {
      "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](http://docs.jsonata.org/path-operators#-context-variable-binding) syntax `@$var` to assign each series definition to a variable name, to avoid the intermediate variables for $xaxis and $yaxis.

---

<div class="post-metadata">

**Author:** ![hwr](https://avatars.discourse-cdn.com/v4/letter/h/c0e974/32.png) [@hwr](https://discourse.nodered.org/u/hwr)\
**Post date:** [21 December 2021 07:15 UTC](https://discourse.nodered.org/t/prepare-array-of-json-objects-with-jsonata-through-change-node-for-node-red-dashboard-multiline-chart/55495/4 "2021-12-21T07:15:26Z")

</div>

Works perfectly, thank you!

```auto
(
  $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)
          }
        )
      ]
    }
  ]
)

```

---

<div class="post-metadata">

**Author:** ![hwr](https://avatars.discourse-cdn.com/v4/letter/h/c0e974/32.png) [@hwr](https://discourse.nodered.org/u/hwr)\
**Post date:** [21 December 2021 08:06 UTC](https://discourse.nodered.org/t/prepare-array-of-json-objects-with-jsonata-through-change-node-for-node-red-dashboard-multiline-chart/55495/5 "2021-12-21T08:06:11Z")

</div>

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:

```auto
{
  "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
    }
  ]
}

```

---

<div class="post-metadata">

**Author:** ![shrickus](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/shrickus/32/517_2.png) [@shrickus](https://discourse.nodered.org/u/shrickus)\
**Post date:** [21 December 2021 13:50 UTC](https://discourse.nodered.org/t/prepare-array-of-json-objects-with-jsonata-through-change-node-for-node-red-dashboard-multiline-chart/55495/6 "2021-12-21T13:50:07Z")

</div>

> [@hwr](#):
>
> the result is just one array with the objects of the three graphs

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

---

<div class="post-metadata">

**Author:** ![hwr](https://avatars.discourse-cdn.com/v4/letter/h/c0e974/32.png) [@hwr](https://discourse.nodered.org/u/hwr)\
**Post date:** [21 December 2021 20:59 UTC](https://discourse.nodered.org/t/prepare-array-of-json-objects-with-jsonata-through-change-node-for-node-red-dashboard-multiline-chart/55495/7 "2021-12-21T20:59:43Z")

</div>

The shortened version works perfectly now, thank you!

jsonata-code for change-node:

```auto
(
  $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)
        }
      ]]
    }
  ]
)

```

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [4 January 2022 21:00 UTC](https://discourse.nodered.org/t/prepare-array-of-json-objects-with-jsonata-through-change-node-for-node-red-dashboard-multiline-chart/55495/8 "2022-01-04T21:00:08Z")

</div>

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