在 Microsoft Flow 中使用数据操作Use data operations with Microsoft Flow

本演练介绍 Microsoft Flow 的部分常用数据操作,例如编辑、联接、选择、筛选数组;创建表;分析 JSON。这些操作是在创建流时用来操作数据的。In this walkthrough, you learn about some of the Microsoft Flow’s popular data operations, such as compose, join, select, filter array, create table, and parse JSON that are available to manipulate data when you create flows.

先决条件Prerequisites

  • 有权访问 Microsoft Flow。Access to Microsoft Flow.
  • PostMan 之类的工具,用于向流发送带 JSON 数组的 HTTP POST 请求。A tool such as PostMan to send HTTP POST requests with a JSON array to your flow.

使用编辑操作Use the compose action

使用“数据操作 - 编辑”(编辑)操作可以在设计流时节省自己的时间,不需多次输入相同的数据。Use the Data Operations - Compose (compose) action to save yourself from entering identical data multiple times when you’re designing a flow. 例如,如果在设计流时需多次输入数组 [0,1,2,3,4,5,6,7,8,9],则可使用编辑操作来节省数组操作时间,如下所示:For example, if you need to enter an array of digits: [0,1,2,3,4,5,6,7,8,9] several times while you design your flow, you could use the compose action to save the array like this:

  1. 搜索“编辑”,然后选择“数据操作 - 编辑”(编辑)操作。Search for Compose, and then select the Data Operations - Compose (compose) action.

    搜索并选择编辑操作

  2. 将数组输入以后要引用的“输入”框中:Enter the array into the Inputs box you want to reference later:

    配置编辑操作

提示

为了方便以后引用,请单击“编辑”卡的标题栏上的文本“编辑”,将“编辑”卡重命名。For easier reference later, rename the Compose card by clicking on the text "Compose" on the title bar of the Compose card.

需要访问编辑操作的内容时,请执行以下步骤,通过“从此流中使用的应用和连接器添加动态内容”列表上的“输出”令牌来操作:When you need to access the contents of the compose action, do so via the Output token on the Add dynamic content from the apps and connectors used in this flow list by following these steps:

  1. 添加一项操作,例如“数据操作 – 联接”。Add an action such as Data Operations – Join.

  2. 选择一个控件,以便向其添加在编辑操作中保存的内容。Select the control to which you’d like to add the contents you saved in the compose action.

    此时会打开“从此流中使用的应用和连接器添加动态内容”The Add dynamic content from the apps and connectors used in this flow opens.

  3. 在“从此流中使用的应用和连接器添加动态内容”上选择“输出”令牌,该令牌位于“动态内容”选项卡的“编辑”类别下。On the Add dynamic content from the apps and connectors used in this flow, select the Output token that’s under the Compose category of the Dynamic content tab.

    使用编辑操作的输出

使用联接操作Use the join action

使用“数据操作 - 联接”操作(联接)可以通过所选分隔符分隔某个数组。Use the Data Operations - Join action (Join) to delimit an array with a separator of your choice. 例如,假设流收到一个 Web 请求,其中包括以下电子邮件地址数组:["d@example.com", "k@example.com", "dal@example.com"]For example, assume your flow receives a web request that includes the following array of email addresses: ["d@example.com", "k@example.com", "dal@example.com"]. 但是,电子邮件程序要求地址是使用分号分隔的单个字符串。However, your email program requires addresses to be a single string that's separated with semicolons. 为此,请执行以下步骤,使用“数据操作 - 联接”(联接)操作将逗号分隔符更改为分号“;”:To do this, use the Data Operations - Join (join) action to change the comma delimiter to a semicolon “;“ by following these steps:

  1. 添加新操作,搜索“联接”,然后选择“数据操作 - 联接”(联接)。Add a new action, search for Join, and then select Data Operations - Join (join).

    搜索并选择联接操作

  2. 将数组输入“从”框,然后将要使用的新分隔符输入“联接方式”框。Enter the array into the From box, and then enter the new delimiter that you want to use into the Join with box.

    在这里,使用的是分号 (;) 作为新的分隔符。Here, I’ve used the semicolon (;) as the new delimiter.

    配置联接操作

  3. 保存并运行流。Save your flow, and then run it.

  4. 流运行后,“数据操作 – 联接”操作的输出将如下所示:After your flow runs, the output of the Data Operations – Join action will be:

    联接输出

使用选择操作Use the select action

使用“数据操作 – 选择”(选择)可转换数组中对象的形状。Use the Data Operations – Select (select) to transform the shape of the objects in an array. 例如,可以添加、删除或重命名数组的每个对象中的元素。For example, you may add, remove, or rename elements in each object in an array.

备注

可以使用选择操作来添加或删除元素,但不能更改数组中对象的数目。While you can add or remove elements using the select action, you cannot change the number of objects in the array.

例如,如果数据通过 Web 请求以下述格式进入流,则可使用选择操作:For example, you can use the select action if data enters your flow via a web request in this format:

[ { "first": "Deon", "last": "Herb" }, { "first": "K", "last": "Herb" } ]

你想要改变传入数据,也就是说,将“first”重命名为“FirstName”,将“last”重命名为“LastName”,并添加名为“FamilyName”的新成员,将“first”和“last”组合在一起(使用空格分隔):and you’d like to reshape the incoming data by renaming "first" to "FirstName", "last" to "LastName" and adding a new member named "FamilyName" that combines "first" and "last" (separated with a space):

[ { "FirstName": "Deon", "FamilyName": "Herb", "FullName": "Deon Herb" }, { "FirstName": "K", "FamilyName": "Herb", "FullName": "K Herb" } ].[ { "FirstName": "Deon", "FamilyName": "Herb", "FullName": "Deon Herb" }, { "FirstName": "K", "FamilyName": "Herb", "FullName": "K Herb" } ].

为此,请执行以下操作:To do this:

  1. 向流添加“请求/响应 – 响应”(请求)操作。Add the Request / Response – Response (request) action to your flow.

  2. 从“请求”卡中选择“使用示例有效负载来生成架构”。Select the Use sample payload to generate schema from the Request card.

  3. 在显示的框中,粘贴源数据数组示例,然后选择“完成”按钮。In the box that displays, paste a sample of your source data array, and then select the Done button.

  4. 添加“数据操作 – 选择”(选择)操作,然后对其进行配置,如下图所示。Add the Data Operations – Select (select) action, and then configure it like the following image.

    配置选择操作

    提示

    选择操作的输出是一个数组,其中包含新成形的对象。The output from the select action is an array that contains the newly shaped objects. 然后即可将该数组用于任何其他操作,例如“编辑”(此前已讨论)。You can then use this array in any other action, such as Compose, discussed earlier.

使用“筛选数组”操作Use the filter array action

使用“数据操作 - 筛选数组”(筛选数组),根据所提供的条件来减少数组中的对象数。Use Data Operations - Filter array (filter array) to reduce the number of objects in an array to a subset that matches the criteria you provide.

备注

不能使用“筛选数组”来更改数组中对象的形状。Filter array cannot be used to change the shape of the objects in an array. 另外,进行筛选的文本区分大小写。Also, the text on which you filter is case sensitive.

例如,可以对以下数组使用“筛选数组”:For example, you can use filter array on this array:

[ { "first": "Deon", "last": "Herb" }, { "first": "K", "last": "Herb" } ]

目的是创建一个新数组,其中只包含“first”设置为“Deon”的对象。to create a new array that contains only objects in which first is set to “Deon”.

让我们执行此操作。Let's do this.

  1. 查找“数据操作 - 筛选数组”(筛选数组)操作,然后将其添加到流。Find, and then add the Data Operations - Filter array (filter array) action to your flow.

  2. 配置“筛选数组”操作,如下图所示。Configure the filter array action like the following image.

    配置“筛选数组”操作

  3. 保存并运行流。Save, and then run your flow.

    可以使用 PostMan 生成一个向流发送 JSON 数组的 Web 请求。You can use PostMan to generate a web request that sends a JSON array to your flow.

  4. 当流运行以后,假设 JSON 输入如以下数组所示:When your flow runs, assuming the JSON input looks like this array:

    [ { "first": "Deon", "last": "Herb" }, { "first": "K", "last": "Herb" } ],[ { "first": "Deon", "last": "Herb" }, { "first": "K", "last": "Herb" } ],

    输出如以下数组所示(注意,仅“first”已设置为“Deon”的对象包括在此操作的输出中):the output looks like this array (notice that only objects in which first is set to “Deon” are included in the output of the action):

    [ { "first": "Deon", "last": "Herb" } ]

使用“创建 CSV 表”操作Use the create csv table action

使用“数据操作 - 创建 CSV 表”(创建 CSV 表)将 JSON 数组输入更改为逗号分隔值 (CSV) 表。Use the Data Operations - Create CSV table (create csv table) to change a JSON array input into a comma separated value (CSV) table. (可选)可以让标头在 CSV 输出中保持可见。Optionally, you can keep the headers visible in the CSV output. 例如,可以使用“创建 CSV 表”操作将以下数组转换为 CSV 表:For example, you can convert the following array into a CSV table by using the Create CSV table action:

[ { "first": "Deon", "last": "Herb" }, { "first": "K", "last": "Herb" } ]

  1. 查找、添加和配置“数据操作 - 创建 CSV 表”操作,如下图所示。Find, add, and then configure the Data Operations - Create CSV table action to resemble the following image.

    配置“创建 CSV 表”操作

    注意:此图像中的“正文”令牌来自“请求请求/响应 – 响应”操作,但你可以在流中从任何前述操作的输出获取“创建 CSV 表”操作的输入,也可以直接将其输入“从”框中。Note: The Body token in this image comes from a Request / Response – Response action, however, you could get the input for the Create CSV table action from the output of any previous action in your flow, or you can enter it directly into the From box.

  2. 保存并运行流。Save, and then run your flow.

    流运行时,“创建 CSV 表”输出如下图所示:When your flow runs, the Create CSV table output looks like this image:

    创建 CSV 表输出

使用“创建 HTML 表”操作Use the create html table action

使用“数据操作 - 创建 HTML 表”将 JSON 数组输入更改为 HTML 表。Use Data Operations - Create HTML table to change a JSON array input into an HTML table. (可选)可以让标头在 HTML 输出中保持可见。Optionally, you can keep the headers visible in the HTML output.

为此,请执行创建 CSV 表部分中的步骤,以获取详细的示例。To do this, follow the steps in the create csv table section for a detailed example. 请确保使用“数据操作 - 创建 HTML 表”操作,而不要使用“数据操作 - 创建 CSV 表”操作。Be sure to use the Data Operations - Create HTML table action, instead of the Data Operations - Create CSV table action.

提示

如果计划通过电子邮件发送 HTML 表,请记住在电子邮件操作中选择“IsHtml”。If you plan to send the HTML table via email, remember to select "IsHtml" in the email action.