# JSONata object creation (with $each?)

**URL:** <https://discourse.nodered.org/t/jsonata-object-creation-with-each/63903>\
**Category:** General\
**Created:** [14 June 2022 09:46 UTC](https://discourse.nodered.org/t/jsonata-object-creation-with-each/63903 "2022-06-14T09:46:54Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Bobblybook](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bobblybook/32/63170_2.png) [@Bobblybook](https://discourse.nodered.org/u/Bobblybook)\
**Post date:** [14 June 2022 09:46 UTC](https://discourse.nodered.org/t/jsonata-object-creation-with-each/63903/1 "2022-06-14T09:46:54Z")

</div>

Hi everyone,  
I'm new to JSONata and I'm struggling to get this right.  
I have some data that I need to transform for graphing in node-red. I've had a look through the documentation and examples, and I'm almost there but I can't quite get object creation working in this context.

Here's a data structure example to illustrate my issue: [https://try.jsonata.org/vGGPlt\_N7](https://try.jsonata.org/vGGPlt_N7)  
However for a line chart in node-red dashboards, the data format needs to be:

```auto
"data": [
      {"x": 1, "y": 13.8},
      {"x": 2, "y": 14.1},
      {"x": 3, "y": 14.4},
      {"x": 4, "y": 14.5},
      {"x": 5, "y": 14.4}
    ],

```

The x values are not fixed, I may choose to put something else there (eg. a timestamp), but for now they are just placeholders to work out the JSONata.

I can't seem to find a way to do this when using $each, as in the above link. However I'm not even really sure I should be using $each - it seems that this takes all the results from each object and flattens them into an array. I actually need to form an object for each measurement so maybe this is counterproductive.

I have also tried something like `"data":temp,` in JSONata, which produces a similar array of values, but I would need to transform these into objects as well.  
I also need to perform division/string manipulation on the values, and I can't do something like `"data":temp / 100,`. I think this is because it would be trying to divide the entire array by 100, and hence doesn't work, whereas I want to actually do an element-wise division of each value. The function definition with $each allows me to do this.

Maybe there is a very simple way of accomplishing this as well as the object issue - it feels like a fairly simple problem but none of the examples or things I've tried so far have really succeeded. Please let me know if anyone has any advice! Thanks.

---

<div class="post-metadata">

**Author:** ![smanjunath211](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/smanjunath211/32/95742_2.png) [@smanjunath211](https://discourse.nodered.org/u/smanjunath211)\
**Post date:** [14 June 2022 11:51 UTC](https://discourse.nodered.org/t/jsonata-object-creation-with-each/63903/2 "2022-06-14T11:51:16Z")

</div>

Welcome...

try this thread, it may lead to what you want..

> [@MySQL to dashboard chart - change function](https://discourse.nodered.org/t/mysql-to-dashboard-chart-change-function/14418/2):
>
> connect a change node to the output of the mysql node. in the change node select the j: expression option (the j: is for jsonata) click the three dots on the right of the tab and it will open up. paste in the code from Steve's example [{ "series": ["kW"], "labels": ["kW Used Today"], "data": [[payload.{ "x": captureTime, "y": $."kW Used Today" }]] }] and in your case change captureTime to time and kW Used Today to value and change the series and labels test t…

---

<div class="post-metadata">

**Author:** ![Bobblybook](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bobblybook/32/63170_2.png) [@Bobblybook](https://discourse.nodered.org/u/Bobblybook)\
**Post date:** [14 June 2022 12:20 UTC](https://discourse.nodered.org/t/jsonata-object-creation-with-each/63903/3 "2022-06-14T12:20:25Z")

</div>

Thanks a lot for this.

```auto
"data": [[$.{
        "x": id,
        "y": temp
    }]],

```

This was enough to match the structure I want. I feel like I tried so many permutations of this sort of syntax but could never get it to parse properly.. I think it's the extra square brackets that I wasn't trying. Thanks again!!

---

<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:** [14 June 2022 13:09 UTC](https://discourse.nodered.org/t/jsonata-object-creation-with-each/63903/4 "2022-06-14T13:09:01Z")

</div>

The issue you have is where is this array , is it in payload? If so then you did not set the input array correct, it needs to be in a object with the property payload.

like this [https://try.jsonata.org/RfHWFmZei](https://try.jsonata.org/RfHWFmZei)

then you would end up with a expression

```auto
[
   {
       "series": ["Temp"],
       "data": [[$$.payload.{"x": $.id*1000, "y": $.temp/100}]],
       "labels":["Temp"]
   }
]

```

Multiplied by 1000 so the times are 1 second apart, edit to fit. A square brackets around an expression will force a single result to an array, so is ignored if the result is an array, that is why you need the extra array bracket.

---

<div class="post-metadata">

**Author:** ![Bobblybook](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bobblybook/32/63170_2.png) [@Bobblybook](https://discourse.nodered.org/u/Bobblybook)\
**Post date:** [14 June 2022 13:53 UTC](https://discourse.nodered.org/t/jsonata-object-creation-with-each/63903/5 "2022-06-14T13:53:44Z")

</div>

Yes sorry, I forgot to mention that I was testing on the [jsonata.org](http://jsonata.org) website and I'm adding in payload on the actual node-red config. I've just spend a little bit adding some rounding and other adjustments to your code, and I'm now getting a proper line graph correctly in the dashboard, so it works!

This syntax is doing my head in. Lots more learning for me.

---

<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:** [28 June 2022 13:54 UTC](https://discourse.nodered.org/t/jsonata-object-creation-with-each/63903/6 "2022-06-28T13:54:34Z")

</div>

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