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.