Hi all,
I have an InfluxDB bucket which stores JSON-formatted data in a field “Fehler”.
I read out the table by:
from(bucket: "Testdaten_1")
|> range(start: -2d, stop: now() )
|> filter(fn: (r) => r["Artikel"] == "Board-Typ 1")
|> filter(fn: (r) => r["Status"] == "failed" or r["Status"] == "aborted")
|> filter(fn: (r) => r["_field"] == "Fehler")
|> keep(columns: ["_value"])
This returns following result:
table _value
0 {"Results":[{"MesswertNr":4,"Titel":"Spannung","MessschrittNr":2,"Min":30,"Max":50,"Actual":13},{"MesswertNr":5,"Titel":"Strom","MessschrittNr":2,"Min":65,"Max":100,"Actual":57}]}
Inspired by the example 2 from Anais’ blog entry, I have written a function which pipes in a table from a query, parse the JSON array and returns a corresponding table:
//returns table with columns "MesswertNr", "NessschrittNr", "Titel", "Min", "Max and "Actual" - values
//see: https://github.com/InfluxCommunity/JSON_Flux/blob/main/example2.Flux
getTableFromJSON = (jsonTable=<-) => jsonTable
|> map(fn: (r) => {
jsonData = json.parse(data: bytes(v: r._value))
// extract the list that we want to map across with array.map
listData = jsonData.Results
// map across each complex type in the array named "Results"
errors = array.map(
arr: listData,
fn: (x) => ({
"MessschrittNr": x.MessschrittNr,
"MesswertNr": x.MesswertNr,
"Titel": x.Titel,
"Min": x.Min,
"Max": x.Max,
"Actual": x.Actual,
})
)
// finally convert that flattened list into a table with array.from
return array.from(rows: errors)
}
)
Piping the query into the function
from(bucket: "Testdaten_1")
|> range(start: -2d, stop: now() )
|> filter(fn: (r) => r["Artikel"] == "Board-Typ 1")
|> filter(fn: (r) => r["Status"] == "failed" or r["Status"] == "aborted")
|> filter(fn: (r) => r["_field"] == "Fehler")
|> keep(columns: ["_value"])
|> getTableFromJSON()
returns
error calling function "getTableFromJSON" @38:6-38:24: error calling function "map" @10:6-29:2: name {"x" ""} does not exist in scope
The part 10:6-29:2 is the map()-function in my function getTableFromJSON ():
|> map(fn: (r) => {
jsonData = json.parse(data: bytes(v: r._value))
// extract the list that we want to map across with array.map
listData = jsonData.Results
// map across each complex type in the array named "Results"
errors = array.map(
arr: listData,
fn: (x) => ({
"MessschrittNr": x.MessschrittNr,
"MesswertNr": x.MesswertNr,
"Titel": x.Titel,
"Min": x.Min,
"Max": x.Max,
"Actual": x.Actual,
})
)
// finally convert that flattened list into a table with array.from
return array.from(rows: errors)
}
)
and 38:6-38:24 is the pipe to the function in the flux query
|> getTableFromJSON()
What is going wrong here?
Is there another way to parse JSON from flux queries?
thx and regards