Skip to content

使用 JMESPath 查询 JSON#

JMESPath 是一种用于 JSON 的查询语言,您可以用它从 JSON 文档中提取和转换元素。关于 JMESPath 的完整使用细节,请参考 JMESPath 文档

jmespath() 方法#

n8n 提供了一个自定义方法 jmespath()。使用此方法可以通过 JMESPath 查询语言在 JSON 对象上执行搜索。

基本语法如下:

1
$jmespath(object, searchString)
1
_jmespath(object, searchString)

为了帮助理解该方法的功能,以下是等效的较长 JavaScript 代码:

1
2
var jmespath = require('jmespath');
jmespath.search(object, searchString);

表达式必须为单行

较长的代码示例在表达式中不起作用,因为表达式必须是单行的。

object 是一个 JSON 对象,例如前一个节点的输出。searchString 是用 JMESPath 查询语言编写的表达式。JMESPath 规范提供了支持的表达式列表,而他们的教程示例则提供了交互式示例。

搜索参数顺序

JMESPath 规范中的示例遵循 search(searchString, object) 模式。n8n 使用的 JMESPath JavaScript 库支持 search(object, searchString)。这意味着当使用 JMESPath 文档中的示例时,您可能需要更改搜索函数参数的顺序。

常见任务#

本节提供一些常见操作的示例。更多示例和详细指南可在 JMESPath 官方文档 中找到。

在尝试这些示例时,您需要将代码节点的模式设置为为每个项目运行一次

使用 JMESPath 投影对元素集合应用表达式#

根据 JMESPath 投影文档

投影是 JMESPath 的核心特性之一。它用于对元素集合应用表达式。JMESPath 支持五种投影类型:

  • 列表投影
  • 切片投影
  • 对象投影
  • 扁平化投影
  • 过滤器投影

以下示例展示了列表、切片和对象投影的基本用法。关于每种投影类型的详细说明和更多示例,请参考 JMESPath 投影文档

假设这是来自 webhook 节点的 JSON 数据:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
[
  {
    "headers": {
      "host": "n8n.instance.address",
      ...
    },
    "params": {},
    "query": {},
    "body": {
      "people": [
        {
          "first": "James",
          "last": "Green"
        },
        {
          "first": "Jacob",
          "last": "Jones"
        },
        {
          "first": "Jayden",
          "last": "Smith"
        }
      ],
      "dogs": {
        "Fido": {
          "color": "brown",
          "age": 7
        },
        "Spot": {
          "color": "black and white",
          "age": 5
        }
      }
    }
  }
]

使用列表投影获取所有人的名字:

1
2
{{$jmespath($json.body.people, "[*].first" )}}
// 返回 ["James", "Jacob", "Jayden"]
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
let firstNames = $jmespath($json.body.people, "[*].first" )
return {firstNames};
/* 返回:
[
	{
		"firstNames": [
			"James",
			"Jacob",
			"Jayden"
		]
	}
]
*/
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
firstNames = _jmespath(_json.body.people, "[*].first" )
return {"firstNames":firstNames}
"""
返回:
[
 	{
		"firstNames": [
			"James",
			"Jacob",
			"Jayden"
		]
	}
]
"""

使用切片投影获取前两个名字:

1
2
{{$jmespath($json.body.people, "[:2].first")}}
// 返回 ["James", "Jacob"]
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
let firstTwoNames = $jmespath($json.body.people, "[:2].first");
return {firstTwoNames};
/* 返回:
[
	{
		"firstNames": [
			"James",
			"Jacob",
			"Jayden"
		]
	}
]
*/
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
firstTwoNames = _jmespath(_json.body.people, "[:2].first" )
return {"firstTwoNames":firstTwoNames}
"""
返回:
[
	{
		"firstTwoNames": [
		"James",
		"Jacob"
		]
	}
]
"""

使用对象投影获取所有狗的年龄:

1
2
{{$jmespath($json.body.dogs, "*.age")}}
// 返回 [7,5]
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
let dogsAges = $jmespath($json.body.dogs, "*.age");
return {dogsAges};
/* 返回:
[
	{
		"dogsAges": [
			7,
			5
		]
	}
]
*/
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
dogsAges = _jmespath(_json.body.dogs, "*.age")
return {"dogsAges": dogsAges}
"""
返回:
[
	{
		"dogsAges": [
			7,
			5
		]
	}
]
"""

选择多个元素并创建新列表或对象#

使用 Multiselect 从 JSON 对象中选择元素并将它们组合成新的列表或对象。

给定来自 webhook 节点的以下 JSON:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
[
  {
    "headers": {
      "host": "n8n.instance.address",
      ...
    },
    "params": {},
    "query": {},
    "body": {
      "people": [
        {
          "first": "James",
          "last": "Green"
        },
        {
          "first": "Jacob",
          "last": "Jones"
        },
        {
          "first": "Jayden",
          "last": "Smith"
        }
      ],
      "dogs": {
        "Fido": {
          "color": "brown",
          "age": 7
        },
        "Spot": {
          "color": "black and white",
          "age": 5
        }
      }
    }
  }
]

使用多选列表获取名字和姓氏并创建包含这两个名称的新列表:

1
2
{{$jmespath($json.body.people, "[].[first, last]")}}
// 返回 [["James","Green"],["Jacob","Jones"],["Jayden","Smith"]]
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
let newList = $jmespath($json.body.people, "[].[first, last]");
return {newList};
/* 返回:
[
	{
		"newList": [
			[
				"James",
				"Green"
			],
			[
				"Jacob",
				"Jones"
			],
			[
				"Jayden",
				"Smith"
			]
		]
	}
]
*/
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
newList = _jmespath(_json.body.people, "[].[first, last]")
return {"newList":newList}
"""
返回:
[
	{
		"newList": [
			[
				"James",
				"Green"
			],
			[
				"Jacob",
				"Jones"
			],
			[
				"Jayden",
				"Smith"
			]
		]
	}
]
"""

表达式中的箭头函数替代方案#

例如,通过从 Code 节点返回以下代码来生成一些输入数据:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
return[
  {
    "json": {      
      "num_categories": "0",
      "num_products": "45",
      "category_id": 5529735,
      "parent_id": 1407340,
      "pos_enabled": 1,
      "pos_favorite": 0,
      "name": "HP",
      "description": "",
      "image": ""
    }
  },
  {
    "json": {
      "num_categories": "0",
      "num_products": "86",
      "category_id": 5529740,
      "parent_id": 1407340,
      "pos_enabled": 1,
      "pos_favorite": 0,
      "name": "Lenovo",
      "description": "",
      "image": ""
    }
  }  
]

您可以执行类似"查找名称为 Lenovo 的项并告诉我其类别 ID"的搜索。

1
{{ $jmespath($("Code").all(), "[?json.name=='Lenovo'].json.category_id") }}