Documentation

json.parse() 函数

json.parse() 是实验性的,随时可能更改

json.parse() 接收JSON数据作为字节并返回一个值。

JSON类型转换为Flux类型如下:

JSON类型Flux类型
布尔型布尔型
数字浮点数
字符串字符串
数组数组
对象记录
函数类型签名
(data: bytes) => A

有关更多信息,请参见 Function type signatures

参数

数据

(必填) 要解析的 JSON 数据(以字节形式)。

示例

解析和使用JSON数据重构表格

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

查看示例输入和输出

解析 JSON 并使用数组函数将其转换为表格

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

查看示例输出



Flux的未来

Flux 正在进入维护模式。您可以像现在一样继续使用它,而无需对您的代码进行任何更改。

阅读更多

InfluxDB 3 开源版本现已公开Alpha测试

InfluxDB 3 Open Source is now available for alpha testing, licensed under MIT or Apache 2 licensing.

我们将发布两个产品作为测试版的一部分。

InfluxDB 3 核心,是我们新的开源产品。 它是一个用于时间序列和事件数据的实时数据引擎。 InfluxDB 3 企业版是建立在核心基础之上的商业版本,增加了历史查询能力、读取副本、高可用性、可扩展性和细粒度安全性。

有关如何开始的更多信息,请查看: