json.parse() function
json.parse()
is experimental and subject to change at any time.
json.parse()
takes JSON data as bytes and returns a value.
JSON types are converted to Flux types as follows:
JSON type | Flux type |
---|---|
boolean | boolean |
number | float |
string | string |
array | array |
object | record |
Function type signature
(data: bytes) => A
Parameters
data
(Required) JSON data (as bytes) to parse.
Examples
- Parse and use JSON data to restructure tables
- Parse JSON and use array functions to manipulate into a table
Parse and use JSON data to restructure tables
import "experimental/json"
data
|> map(
fn: (r) => {
jsonData = json.parse(data: bytes(v: r._value))
return {
_time: r._time,
_field: r._field,
a: jsonData.a,
b: jsonData.b,
c: jsonData.c,
}
},
)
Parse JSON and use array functions to manipulate into a table
import "experimental/json"
import "experimental/array"
jsonStr =
bytes(
v:
"{
\"node\": {
\"items\": [
{
\"id\": \"15612462\",
\"color\": \"red\",
\"states\": [
{
\"name\": \"ready\",
\"duration\": 10
},
{
\"name\": \"closed\",
\"duration\": 13
},
{
\"name\": \"pending\",
\"duration\": 3
}
]
},
{
\"id\": \"15612462\",
\"color\": \"blue\",
\"states\": [
{
\"name\": \"ready\",
\"duration\": 5
},
{
\"name\": \"closed\",
\"duration\": 0
},
{
\"name\": \"pending\",
\"duration\": 16
}
]
}
]
}
}",
)
data = json.parse(data: jsonStr)
// Map over all items in the JSON extracting
// the id, color and pending duration of each.
// Construct a table from the final records.
array.from(
rows:
data.node.items
|> array.map(
fn: (x) => {
pendingState =
x.states
|> array.filter(fn: (x) => x.name == "pending")
pendingDur =
if length(arr: pendingState) == 1 then
pendingState[0].duration
else
0.0
return {id: x.id, color: x.color, pendingDuration: pendingDur}
},
),
)
Was this page helpful?
Thank you for your feedback!
Support and feedback
Thank you for being part of our community! We welcome and encourage your feedback and bug reports for Flux and this documentation. To find support, use the following resources:
Customers with an annual or support contract can contact InfluxData Support.