docs-v2/content/flux/v0/stdlib/experimental/json/parse.md

5.5 KiB

title description menu weight flux/v0/tags
json.parse() function `json.parse()` takes JSON data as bytes and returns a value.
flux_v0_ref
name parent identifier
json.parse experimental/json experimental/json/parse
201
type-conversions

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

{{% 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

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:00Z foo {"a":1,"b":2,"c":3}
2021-01-01T01:00:00Z foo {"a":4,"b":5,"c":6}
2021-01-01T02:00:00Z foo {"a":7,"b":8,"c":9}
2021-01-01T00:00:00Z bar {"a":10,"b":9,"c":8}
2021-01-01T01:00:00Z bar {"a":7,"b":6,"c":5}
2021-01-01T02:00:00Z bar {"a":4,"b":3,"c":2}

Output data

_field _time a b c
foo 2021-01-01T00:00:00Z 1 2 3
foo 2021-01-01T01:00:00Z 4 5 6
foo 2021-01-01T02:00:00Z 7 8 9
bar 2021-01-01T00:00:00Z 10 9 8
bar 2021-01-01T01:00:00Z 7 6 5
bar 2021-01-01T02:00:00Z 4 3 2

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

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

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

Output data

id color pendingDuration
15612462 red 3
15612462 blue 16

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