提取YAML节点
概述
Extract YAML节点用于从字符串中解析YAML对象。当您需要从包含YAML对象的文本字符串中提取结构化数据时,它特别有用。
该节点会在输入文本中查找预定义的根属性名称,并将该名称之后所有缩进的文本视为YAML对象的一部分。默认情况下,根属性名称设置为yamlDocument
,但可以通过节点设置或输入端口进行配置。
您还可以使用JSONPath表达式指定YAML对象中特定属性的路径。该节点随后将从YAML对象中提取该属性的值。这是在提取YAML节点后使用提取对象路径节点的简写方式。
请注意,整个文档包含根属性名称。例如,如果根属性名称设置为yamlDocument
,那么对象路径$.yamlDocument
将返回整个文档。
Extract YAML节点只能从字符串中提取单个YAML对象。如果您想从字符串中提取多个YAML对象,可能需要使用Code Node或External Call Node来进行更高级的文本提取。
YAML对象之外的文本将被忽略。如果您想提取YAML对象之外的文本,可以使用Extract with Regex Node代替。
- 输入
- 输出
- 编辑器设置
输入项
标题 | 数据类型 | 描述 | 默认值 | 备注 |
---|---|---|---|---|
Input | string | The string that contains the YAML object. | (required) | The input will be coerced into a string if it is not a string. |
Root Property Name | string | The root property name of the YAML object in the input string. | (optional) | The input will be coerced into a string if it is not a string. This input is only available if enabled in the node settings. |
Object Path | string | The JSONPath expression to extract data from the parsed YAML object. | (optional) | The input will be coerced into a string if it is not a string. This input is only available if enabled in the node settings. |
输出
标题 | 数据类型 | 描述 | 备注 |
---|---|---|---|
Output | object | The parsed YAML object or the value extracted from the object using the JSONPath expression. | If the YAML object or the value is not found, this port will not be ran. |
Matches | any[] | When using a JSONPath expression that can match multiple times, represents all paths of the object that match. | If the YAML object or the value is not found, this port will run with an empty array. |
No Match | string | If no YAML object is found in the input text, this port will run, with the full contents of the input string. | If a YAML object is found in the input string, this port will not be ran. |
编辑器设置
设置 | 描述 | 默认值 | 使用输入切换 | 输入数据类型 |
---|---|---|---|---|
Root Property Name | The root property name of the YAML object in the input string. | yamlDocument | Yes | string |
Object Path | The JSONPath expression to extract data from the parsed YAML object. | (empty) | Yes | string |
示例1:从字符串中提取YAML对象
创建一个文本节点并将文本设置为以下内容:
yamlDocument:
name: John Doe
age: 30
job: Engineer创建一个Extract YAML节点,并将文本节点连接到其
Input
输入端口。运行图表。Extract YAML节点的
Output
应包含解析后的YAML对象。
示例2:从YAML对象中提取属性
创建一个文本节点并将文本设置为以下内容:
yamlDocument:
name: John Doe
age: 30
job: Engineer创建一个Extract YAML节点,并将文本节点连接到其
Input
输入端口。将Extract YAML Node的
Object Path
设置为$.yamlDocument.name
。运行图表。Extract YAML节点的
Output
应包含值John Doe
。
错误处理
如果JSONPath表达式无效,Extract YAML Node将会报错。如果未找到YAML对象或值,则不会运行Output
和Matches
输出,而No Match
输出将运行并包含输入字符串的完整内容。
常见问题
问:我可以使用Extract YAML Node从JSON对象中提取数据吗?
A: 不,Extract YAML Node 是专门设计用于解析 YAML 对象的。如果您想从 JSON 对象中提取数据,可以使用 Extract JSON Node 代替。
问:我可以使用Extract YAML Node从YAML对象中提取数组吗?
A: 是的,您可以使用Extract YAML节点来提取任何有效的YAML结构,包括数组。您可以使用JSONPath表达式来指定YAML对象中数组的路径。或者,您可以指定一个多重匹配,例如$.yamlDocument.someArray[*]
并使用Matches
输出来获取所有匹配项。