JSONata object creation (with $each?)

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
However for a line chart in node-red dashboards, the data format needs to be:

"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.


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

Thanks a lot for this.

"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!!

1 Like

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

then you would end up with a expression

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

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.

Yes sorry, I forgot to mention that I was testing on the 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.

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