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")}}
// Returns [7,5]
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
let dogsAges = $jmespath($json.body.dogs, "*.age");
return {dogsAges};
/* Returns:
[
	{
		"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}
"""
Returns:
[
	{
		"dogsAges": [
			7,
			5
		]
	}
]
"""

Select multiple elements and create a new list or object#

Use Multiselect to select elements from a JSON object and combine them into a new list or object.

Given this JSON from a webhook node:

 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
        }
      }
    }
  }
]

Use multiselect list to get the first and last names and create new lists containing both names:

1
2
{{$jmespath($json.body.people, "[].[first, last]")}}
// Returns [["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};
/* Returns:
[
	{
		"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}
"""
Returns:
[
	{
		"newList": [
			[
				"James",
				"Green"
			],
			[
				"Jacob",
				"Jones"
			],
			[
				"Jayden",
				"Smith"
			]
		]
	}
]
"""

An alternative to arrow functions in expressions#

For example, generate some input data by returning the below code from the Code node:

 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": ""
    }
  }  
]

You could do a search like "find the item with the name Lenovo and tell me their category ID."

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