使用 JMESPath 查询 JSON(Query JSON with JMESPath)#
JMESPath 是一种用于 JSON 的查询语言,您可以使用它从 JSON 文档中提取和转换元素。有关如何使用 JMESPath 的完整详细信息,请参阅 JMESPath 文档。
jmespath() 方法(The jmespath() method)#
n8n 提供了一个自定义方法 jmespath()。使用此方法可以使用 JMESPath 查询语言在 JSON 对象上执行搜索。
🌐 n8n provides a custom method, jmespath(). Use this method to perform a search on a JSON object using the JMESPath query language.
基本语法为:
🌐 The basic syntax is:
=== “JavaScript”
1 2 3 | |
1 | |
为了帮助你理解该方法的作用,以下是等效的更长的 JavaScript 代码:
🌐 To help understand what the method does, here is the equivalent longer JavaScript:
1 2 | |
表达式必须是单行的
较长的代码示例在表达式中不起作用,因为它们必须是单行的。
object 是一个 JSON 对象,例如前一个节点的输出。searchString 是用 JMESPath 查询语言编写的表达式。JMESPath 规范 提供了支持的表达式列表,而它们的 教程 和 示例 提供了交互式示例。
搜索参数顺序
JMESPath 规范 中的示例遵循 search(searchString, object) 模式。而 n8n 使用的 JMESPath JavaScript 库 则支持 search(object, searchString)。这意味着在使用 JMESPath 文档中的示例时,你可能需要更改搜索函数参数的顺序。
常见任务(Common tasks)#
本节提供了一些常见操作的示例。更多示例和详细指南可参阅 JMESPath 的官方文档。
🌐 This section provides examples for some common operations. More examples, and detailed guidance, are available in JMESPath's own documentation.
在尝试这些示例时,你需要将代码节点的 模式 设置为 对每个项运行一次。
🌐 When trying out these examples, you need to set the Code node Mode to Run Once for Each Item.
将 JMESPath 表达式应用于带有投影的元素集合(Apply a JMESPath expression to a collection of elements with projections)#
来自 JMESPath 投影文档:
🌐 From the JMESPath projections documentation:
投影是 JMESPath 的一个关键功能。使用它可以将表达式应用于一组元素。JMESPath 支持五种投影类型:
- 列表投影
- 切片投影
- 对象投影
- 扁平化投影
- 过滤投影
以下示例展示了列表、切片和对象投影的基本用法。有关每种投影类型的详细说明以及更多示例,请参阅 JMESPath 投影文档。
🌐 The following example shows basic usage of list, slice, and object projections. Refer to the JMESPath projections documentation for detailed explanations of each projection type, and more examples.
给定来自 webhook 节点的以下 JSON:
🌐 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 | |
获取所有人的名字列表:列表:
🌐 Retrieve a list of all the people's first names:
=== “表达式(JavaScript)”
1 2 3 4 | |
=== “代码节点(JavaScript)”
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | |
获取名字的一部分:
🌐 Get a slice of the first names:
=== “表达式(JavaScript)”
1 2 3 4 | |
=== “代码节点(JavaScript)”
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | |
1 2 3 4 5 6 7 8 9 10 11 12 13 | |
使用 对象投影 获取狗的年龄列表:
🌐 Get a list of the dogs' ages using object projections:
=== “表达式(JavaScript)”
1 2 3 4 | |
=== “代码节点(JavaScript)”
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | |
1 2 3 4 5 6 7 8 9 10 11 12 13 | |
选择多个元素并创建新列表或对象(Select multiple elements and create a new list or object)#
使用 多选 从 JSON 对象中选择元素,并将它们组合成新的列表或对象。
🌐 Use Multiselect to select elements from a JSON object and combine them into a new list or object.
给定来自 webhook 节点的以下 JSON:
🌐 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 | |
使用多选列表获取名和姓,并创建包含两者的新列表:
=== “表达式(JavaScript)”
1 2 3 4 | |
=== “代码节点(JavaScript)”
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | |
箭头函数的替代方案表达式(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 | |
你可以进行这样的搜索:“查找名称为 Lenovo 的物品,并告诉我它们的类别 ID。”
🌐 You could do a search like "find the item with the name Lenovo and tell me their category ID."
1 | |