产品定价 立即试用
社区版
入门 文档 指南 安装 架构 API 常见问题

json path

使用JSONPath表达式从入站消息 data 中提取一部分,并将其设为新的消息data。

前置条件

入站消息 data 必须为有效JSON对象或数组。若data不是有效JSON,消息将经 Failure 链路由。

配置

字段说明

  • JSONPath — 用于查询入站消息data的JSONPath表达式。该查询结果将完全替换原始消息data。
    • 默认值为 $,表示整个根JSON对象或数组。使用默认值将使原始消息原样透传。

JSON Schema

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "title": "TbJsonPathNodeConfiguration",
  "type": "object",
  "properties": {
    "jsonPath": {
      "type": "string",
      "description": "The JSONPath expression to apply to the message data."
    }
  },
  "required": [
    "jsonPath"
  ],
  "additionalProperties": false
}

消息处理逻辑

  1. 节点将配置的 JSONPath 表达式应用到入站消息data。
  2. 若表达式成功找到一个或多个元素:
    • 消息的 data 被JSONPath查询结果替换。
    • 消息经 Success 链发送。
  3. 若配置的JSONPath在消息data中未找到,原始消息经 Failure 链发送。
  4. 若入站消息data不是有效JSON,消息经 Failure 链发送。

输出连接

  • Success
    • JSONPath表达式成功求值时。
  • Failure
    • 消息data不是有效JSON时。
    • 消息data中未找到指定的JSONPath表达式时。
    • 处理过程中发生意外错误时。

示例

以下示例仅展示入站消息、规则节点配置和系统状态的相关部分。


示例1 — 选择嵌套属性

入站消息

Data:

1
2
3
4
5
6
7
8
9
10
{
  "device": {
    "name": "Thermostat A1",
    "type": "TH-01"
  },
  "telemetry": {
    "temperature": 23.5,
    "humidity": 55
  }
}

节点配置

1
2
3
{
  "jsonPath": "$.telemetry"
}

出站消息

Data:

1
2
3
4
{
  "temperature": 23.5,
  "humidity": 55
}

输出连接: Success

说明: JSONPath表达式 $.telemetry 选中 telemetry 对象。该对象成为新的消息data,替换原始内容。


示例2 — 选择数组元素

入站消息

Data:

1
2
3
4
5
6
7
8
9
10
11
12
{
  "device_profiles": [
    {
      "name": "default",
      "isDefault": true
    },
    {
      "name": "low_power",
      "isDefault": false
    }
  ]
}

节点配置

1
2
3
{
  "jsonPath": "$.device_profiles[1].name"
}

出站消息

Data:

1
"low_power"

输出连接: Success

说明: 表达式从 device_profiles 数组的第二个元素(索引1)选中 name 属性。结果字符串 "low_power" 成为新的消息data。


示例3 — 路径未找到

入站消息

Data:

1
2
3
{
  "temperature": 25.1
}

节点配置

1
2
3
{
  "jsonPath": "$.humidity"
}

出站消息

原始消息经 Failure 链路由。

输出连接: Failure

说明: 消息data根级别不包含 humidity 键。因路径未找到,消息视为失败。


示例4 — 使用过滤器选择子集

入站消息

Data:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
{
  "readings": [
    {
      "type": "temperature",
      "value": 19
    },
    {
      "type": "humidity",
      "value": 78
    },
    {
      "type": "temperature",
      "value": 21
    }
  ]
}

节点配置

1
2
3
{
  "jsonPath": "$.readings[?(@.type == 'temperature')]"
}

出站消息

Data:

1
2
3
4
5
6
7
8
9
10
[
  {
    "type": "temperature",
    "value": 19
  },
  {
    "type": "temperature",
    "value": 21
  }
]

输出连接: Success

说明: JSONPath过滤器表达式 [?(@.type == 'temperature')] 选中 readings 数组中 type 为 “temperature” 的所有对象。结果数组成为新的消息data。