I am trying to convert query results into the correct form to display in a chart in node-red.
The data comes in an array results of objects and each object contains the details of one time series.
For the chart to display all series together the data needs to be converted into an "array of arrays of objects (the actual x/y points)".
I am able to to this if the source JSON contains more than one time series, but if there is only one it fails.
The working example looks like this, whereas the array results can contain n (between 0 and N) elements.
(See example with n=2 in JSONata Exerciser https://try.jsonata.org/2GqaqME4F)
{
"payload": {
"results": [
{
"statement_id": 0,
"series": [
{
"name": "table1",
"columns": [
"time",
"temperature 1"
],
"values": [
[
"2022-08-18T14:55:00Z",
1.1
],
[
"2022-08-18T15:00:00Z",
1.2
]
]
}
]
},
{
"statement_id": 1,
"series": [
{
"name": "table1",
"columns": [
"time",
"temperature 2"
],
"values": [
[
"2022-08-18T14:55:00Z",
2.1
],
[
"2022-08-18T15:00:00Z",
2.2
]
]
}
]
}
]
}
}
I'm converting the data right now with this JSONata
[
{
"series": [ $.payload.results.($.series.columns[1]) ],
"labels": [ $.payload.results.("") ],
"data": [
$.payload.results.[series.values.( $[1] != null ? {"x": $[0].$toMillis(),"y": $[1]} )]
]
}
]
and it gives me the expected results for n>1:
[
{
"series": [
"temperature 1",
"temperature 2"
],
"labels": [
"",
""
],
"data": [
[
{
"x": 1660834500000,
"y": 1.1
},
{
"x": 1660834800000,
"y": 1.2
}
],
[
{
"x": 1660834500000,
"y": 2.1
},
{
"x": 1660834800000,
"y": 2.2
}
]
]
}
]
For the chart to display all series together it is important that data is an "array of arrays of objects (the actual x/y points)".
For n=1 (see for n=1 in JSONata Exerciser https://try.jsonata.org/F0prxQW4o)
{
"payload": {
"results": [
{
"statement_id": 0,
"series": [
{
"name": "table1",
"columns": [
"time",
"temperature 1"
],
"values": [
[
"2022-08-18T14:55:00Z",
1.1
],
[
"2022-08-18T15:00:00Z",
1.2
]
]
}
]
}
]
}
}
the result is
[
{
"series": [
"temperature 1"
],
"labels": [
""
],
"data": [
{
"x": 1660834500000,
"y": 1.1
},
{
"x": 1660834800000,
"y": 1.2
}
]
}
]
which is no longer an "array of arrays of objects", but an "array of objects".
Result should be
[
{
"series": [
"temperature 1"
],
"labels": [
""
],
"data": [
[ //<--- This parenthesis is missing
{
"x": 1660834500000,
"y": 1.1
},
{
"x": 1660834800000,
"y": 1.2
}
] //<--- This parenthesis is missing
]
}
]
Could somebody tell me how a JSONata would have to look like to work for all n between 0 and N?