# JSONATA for chart

**URL:** <https://discourse.nodered.org/t/jsonata-for-chart/61005>\
**Category:** General\
**Tags:** chart\
**Created:** [8 April 2022 20:40 UTC](https://discourse.nodered.org/t/jsonata-for-chart/61005 "2022-04-08T20:40:09Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![rex](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/rex/32/59848_2.png) [@rex](https://discourse.nodered.org/u/rex)\
**Post date:** [8 April 2022 20:40 UTC](https://discourse.nodered.org/t/jsonata-for-chart/61005/1 "2022-04-08T20:40:09Z")

</div>

I’m working with charts. The acquired data return three measures for each reading.  
This is a sample of some of these.

```auto
[
  {
    "m_devID": 1,
    "m_time": "2022-04-04T13:48:11.000Z",
    "temperature": "17.65",
    "cond": "2900",
    "hum": "4.36"
  },
  {
    "m_devID": 1,
    "m_time": "2022-04-04T14:08:11.000Z",
    "temperature": "17.59",
    "cond": "2900",
    "hum": "4.36"
  },
  {
    "m_devID": 1,
    "m_time": "2022-04-04T14:28:11.000Z",
    "temperature": "17.54",
    "cond": "2900",
    "hum": "4.36"
  }
]

```

I transform the data in order to be used with chart, to di so I’ve implemented one for cycle and I build what I need.  
These data are the transformed ones.

```auto
[
  {
    "series": [
      "Temp °c",
      "Hum. %",
      "Cond mS/cm"
    ],
    "data": [
      [
        {
          "x": 1649080091000,
          "y": 17.65
        },
        {
          "x": 1649081291000,
          "y": 17.59
        },
        {
          "x": 1649082491000,
          "y": 17.54
        }
      ],
      [
        {
          "x": 1649080091000,
          "y": 4.36
        },
        {
          "x": 1649081291000,
          "y": 4.36
        },
        {
          "x": 1649082491000,
          "y": 4.36
        }
      ],
      [
        {
          "x": 1649080091000,
          "y": 2.9
        },
        {
          "x": 1649081291000,
          "y": 2.9
        },
        {
          "x": 1649082491000,
          "y": 2.9
        }
      ]
    ],
    "labels": [
      "ltemp",
      "lhum",
      "lcond"
    ]
  }
]

```

I’m convinced that is possible to do the same whit a simpler JSONATA operation, but I wasn’t able to find one. I’m wrong, does exist a simple JSONATA transform?

F.

---

<div class="post-metadata">

**Author:** ![Paul-Reed](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/paul-reed/32/66906_2.png) [@Paul-Reed](https://discourse.nodered.org/u/Paul-Reed)\
**Post date:** [8 April 2022 20:46 UTC](https://discourse.nodered.org/t/jsonata-for-chart/61005/2 "2022-04-08T20:46:39Z")

</div>

Is there a particular reason for wanting to use jsonata?

Jsonata often looks as though it's more efficient because it uses less code, but as mentioned a number of times in the forum, it can take considerably more time to execute the code, than plain old JavaScript.

Just something to consider.

---

<div class="post-metadata">

**Author:** ![rex](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/rex/32/59848_2.png) [@rex](https://discourse.nodered.org/u/rex)\
**Post date:** [8 April 2022 20:56 UTC](https://discourse.nodered.org/t/jsonata-for-chart/61005/3 "2022-04-08T20:56:04Z")

</div>

> [@Paul-Reed](#):
>
> Jsonata often looks as though it's more efficient because it uses less code, but as mentioned a number of times in the forum, it can take considerably more time to execute the code, than plain old JavaScript.
> 
> Just something to consider.

No, I solved the problem in the traditional way, but I've spent a lot of time trying (without a solution) with jsonata, and now I'm curious. 🙄

---

<div class="post-metadata">

**Author:** ![rex](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/rex/32/59848_2.png) [@rex](https://discourse.nodered.org/u/rex)\
**Post date:** [9 April 2022 13:14 UTC](https://discourse.nodered.org/t/jsonata-for-chart/61005/4 "2022-04-09T13:14:09Z")

</div>

When I stopped to look for solutions I found, at least one.

Here it is:

```auto
[
   {
       "labels":["Humidity", "Conductivity", "Temperature"],
       "series":["%", "mS/cm", "°C"],
       "data": [
           [
               /* 
               Signature: $map(array, function)
               function(value [, index [, array]])

               $v is the value of m_time, $i is the array index, $a is the array (not needed here) 
               */
               
               $map(
                   payload.$toMillis(m_time),
                   function($v, $i, $a) {   
                       {
                           'x': $v,
                           'y':payload[$i].$number(hum) 
                }  
            }
            )    
        ],[
               $map(
                   payload.$toMillis(m_time),
                   function($v, $i) {
                       {
                           'x': $v,
                           'y':payload[$i].$number(cond) /1000
                }  
            }
            )    
        ],[
               $map(
                   payload.$toMillis(m_time),
                   function($v, $i) {
                       {
                           'x': $v,
                           'y':payload[$i].$number(temperature)
                }  
            }
            )    
        ]
       ]
   }
]

```

It needs three runs over payload so, as suggested by @Paul-Reed, I think is not so efficient. Maybe I make some performance tests.

Applied to this data

```auto
{
  "payload": [
    {
      "m_devID": 1,
      "m_time": "2022-04-04T13:48:11.000Z",
      "temperature": "17.65",
      "cond": "2900",
      "hum": "4.36"
    },
    {
      "m_devID": 1,
      "m_time": "2022-04-04T14:08:11.000Z",
      "temperature": "17.59",
      "cond": "2900",
      "hum": "4.36"
    },
    {
      "m_devID": 1,
      "m_time": "2022-04-04T14:28:11.000Z",
      "temperature": "17.54",
      "cond": "2900",
      "hum": "4.36"
    }
  ]
}

```

Look here:  
[https://try.jsonata.org/J26OuQ1JW](https://try.jsonata.org/J26OuQ1JW)

---

<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:** [9 April 2022 13:35 UTC](https://discourse.nodered.org/t/jsonata-for-chart/61005/5 "2022-04-09T13:35:36Z")

</div>

Which could be reduced to

```auto
[
   {
       "labels":["Humidity", "Conductivity", "Temperature"],
       "series":["%", "mS/cm", "°C"],
       "data": [
           $map(["hum","cond","temperature"], function($v){
           [$$.payload.{
                'x': $toMillis($.m_time),
                'y': $number($lookup($,$v)) 
            }  
        ]})
       ]
   }
]

```

---

<div class="post-metadata">

**Author:** ![Paul-Reed](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/paul-reed/32/66906_2.png) [@Paul-Reed](https://discourse.nodered.org/u/Paul-Reed)\
**Post date:** [9 April 2022 13:41 UTC](https://discourse.nodered.org/t/jsonata-for-chart/61005/6 "2022-04-09T13:41:11Z")

</div>

> [@rex](#):
>
> Maybe I make some performance tests.

This node is ideal for checking performance times - [GitHub - bartbutenaers/node-red-contrib-msg-profiler: A Node-RED node to execute time profiling (and tracing) of a message through a flow](https://github.com/bartbutenaers/node-red-contrib-msg-profiler)

---

<div class="post-metadata">

**Author:** ![rex](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/rex/32/59848_2.png) [@rex](https://discourse.nodered.org/u/rex)\
**Post date:** [9 April 2022 18:51 UTC](https://discourse.nodered.org/t/jsonata-for-chart/61005/7 "2022-04-09T18:51:41Z")

</div>

> [@E1cid](#):
>
> ```auto
> [
> {
> "labels":["Humidity", "Conductivity", "Temperature"],
> "series":["%", "mS/cm", "°C"],
> "data": [
> $map(["hum","cond","temperature"], function($v){
> [$.payload.{
> 'x': $toMillis($.m_time),
> 'y': $number($lookup($,$v)) 
> }  
> ]})
> ]
> }
> ]
> 
> ```

Thanks. Very concise query. If I understand it cycles three times, one for every parameter inside the array ["hum","cond","temperature"].  
I've to better understand JSONATA and the use of parameters, and the documentation isn't very easy. Too much $ 🙂.

---

<div class="post-metadata">

**Author:** ![rex](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/rex/32/59848_2.png) [@rex](https://discourse.nodered.org/u/rex)\
**Post date:** [9 April 2022 18:52 UTC](https://discourse.nodered.org/t/jsonata-for-chart/61005/8 "2022-04-09T18:52:37Z")

</div>

@Paul-Reed very useful node.

---

<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:** [8 June 2022 18:53 UTC](https://discourse.nodered.org/t/jsonata-for-chart/61005/9 "2022-06-08T18:53:30Z")

</div>

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