Documentation

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 typeFlux type
booleanboolean
numberfloat
stringstring
arrayarray
objectrecord
Function type signature
(data: bytes) => A
For more information, see Function type signatures.

Parameters

data

(Required) JSON data (as bytes) to parse.

Examples

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

View example input and output

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

View example output


Was this page helpful?

Thank you for your feedback!


Introducing InfluxDB 3.0

The new core of InfluxDB built with Rust and Apache Arrow. Available today in InfluxDB Cloud Dedicated.

Learn more

State of the InfluxDB Cloud Serverless documentation

The new documentation for InfluxDB Cloud Serverless is a work in progress. We are adding new information and content almost daily. Thank you for your patience!

If there is specific information you’re looking for, please submit a documentation issue.