Back to Influxdb

json.parse() function

content/flux/v0/stdlib/experimental/json/parse.md

latest5.2 KB
Original Source
<!------------------------------------------------------------------------------ IMPORTANT: This page was generated from comments in the Flux source code. Any edits made directly to this page will be overwritten the next time the documentation is generated. To make updates to this documentation, update the function comments above the function definition in the Flux source code: https://github.com/influxdata/flux/blob/master/stdlib/experimental/json/json.flux#L136-L136 Contributing to Flux: https://github.com/influxdata/flux#contributing Fluxdoc syntax: https://github.com/influxdata/flux/blob/master/docs/fluxdoc.md ------------------------------------------------------------------------------->

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
js
(data: bytes) => A

{{% caption %}} For more information, see Function type signatures. {{% /caption %}}

Parameters

data

({{< req >}}) JSON data (as bytes) to parse.

Examples

Parse and use JSON data to restructure tables

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

{{< expand-wrapper >}} {{% expand "View example input and output" %}}

Input data

_time_field_value
2021-01-01T00:00:00Zfoo{"a":1,"b":2,"c":3}
2021-01-01T01:00:00Zfoo{"a":4,"b":5,"c":6}
2021-01-01T02:00:00Zfoo{"a":7,"b":8,"c":9}
2021-01-01T00:00:00Zbar{"a":10,"b":9,"c":8}
2021-01-01T01:00:00Zbar{"a":7,"b":6,"c":5}
2021-01-01T02:00:00Zbar{"a":4,"b":3,"c":2}

Output data

_field_timeabc
foo2021-01-01T00:00:00Z123
foo2021-01-01T01:00:00Z456
foo2021-01-01T02:00:00Z789
bar2021-01-01T00:00:00Z1098
bar2021-01-01T01:00:00Z765
bar2021-01-01T02:00:00Z432

{{% /expand %}} {{< /expand-wrapper >}}

Parse JSON and use array functions to manipulate into a table

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

{{< expand-wrapper >}} {{% expand "View example output" %}}

Output data

idcolorpendingDuration
15612462red3
15612462blue16

{{% /expand %}} {{< /expand-wrapper >}}