Parse JSON from Flux Query

Hello @domsch,
I see that the following works for me:

import "array"
import "experimental/json"

myJSON = "{\"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}]}"

jsonData = json.parse(data: bytes(v: myJSON))
 listData = jsonData.Results

// array.from(rows: [{_value: display(v:listData)}])

 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,
      })
    )

array.from(rows: errors)

I think the issue here is you need a way to name each table stream for each table that’s produced from array.from(). The map function must return an object, not a stream.

Can you share two rows of your original data? Berfore you apply your function in annotated csv please?

I’m not sure how to solve this.
@scott do you have any ideas?

Here’s an example script to work with:

import "array"
import "experimental/json"

array.from(rows: [{_value: "{\"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}]}"}, {_value: "{\"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}]}"}])


 |> 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,
//       })
//     )
// 
//     final =  array.from(rows: errors) |> group() 
    // finally convert that flattened list into a table with array.from 
    return array.from(rows:[{value: display(v: listData)}]) |> group() 
}