We have a system that returns timeseries in a rather strange manner -- in a single object with separated Values
and TimeStamps
, e.g:
[
{
"Location": "a2a127e8-0000-0000-0000-5dd95f33203b/1",
"Keyinfo": {
"Name": "Temperature"
},
"Values": [
20.4627780914307,
20.4127216339111,
20.3624877929688
],
"TimeStamps": [
"2023-06-21T00:57:54+06:00",
"2023-06-21T04:38:02+06:00",
"2023-06-21T04:52:33+06:00"
]
}
]
Full example for tests https://pastebin.com/raw/TdtqdGM3
It is pretty simple to fix it with any programming language (e.g, using a proxy or other middleware), but I want to stay using direct connection via JSON API datasource.
Is it possible to bring it to the desired form with one or more transformations? with built in tools or with another plugins. Are there plugins with custom coding?
grafana version 10.0.0
Given that we are talking about JSON, we can try to use such a powerful tool as Jsonata. So we can take a datasource plugin with Jsonata support, e.g JSON API plugin.
Then for the given example we can try to transform single object to many iterating over e.g timestamps:
(
/* Remember mattering values in variables */
$location := Location;
$values := Values;
$name := Keyinfo.Name;
/* Loop over timestamps */
TimeStamps ~> $map(function($v, $i){
/* $v - is an iterating objects (timestamps) */
/* $i - is incrementing index */
{
"Location": $location,
"Name": $name,
"TimeStamp": $v,
"Value": $values[$i]
}
})
)
Full example for pastebined code: https://try.jsonata.org/os1cm_IWU
So we have a list of objects in our result and we have to extract wanted values from this frame, like this: