将数组转换为对象(预览)

模板 ID:convertarraytoobject

将正文中的数组转换为具有用户指定键所标识的元素的 JSON 对象。 此模板在 Power Apps 和 Power Automate 中可用。

若要开始,请在数组所在的父级内指定父对象或集合的路径以及属性子路径。 接下来,将数组每个项中的属性指定为对象键。 这样一个键的存在对于转换具有意义是必要的,因为对象键应该是唯一的,并且该对象键应该解释其余的属性,这将成为值的一部分。 最后,指定将在其中写入新对象属性的路径。

需要注意的几个事项:

  • 如果父级是要转换的数组,您可以将子路径保留为空。
  • 若要覆盖现有数组,路径将是先前指定的父路径和属性子路径。
  • 若要将指定的对象键保留为新对象内的属性,将“保留键”选项设置为“true”(如果保留为空,这是默认值)。 否则,将此选项设置为“false”。

示例

让我们看一下使用以下参数的一些示例。

示例 1

输入 JSON:

    {
        "peopleArray":[
            {
               "Name": "XYZ",
               "Age": "30"
            },
            {
               "Name": "ABC",
               "Age": "23"
            }
        ]
    }
输入参数
propertyParentPath@body()
propertySubPathpeopleArray
keyWithinCollectionPath客户
newPropertyPathpeopleObject
retainKey

输出 JSON:

    {
        "peopleArray":[
            {
               "Name": "XYZ",
               "Age": "30"
            },
            {
               "Name": "ABC",
               "Age": "23"
            }
        ],
        "peopleObject":{
            "XYZ":{
                "Age": "30"
            },
            "ABC":{
                "Age": "23"
            }
        }
    }

示例 2

输入 JSON

    {
        "sets":[
            {
                "people":[
                    {
                        "Name": "XYZ",
                        "Age": "30"
                    },
                    {
                        "Name": "ABC",
                        "Age": "23"
                    }
                ]
            },
            {
                "people":[
                    {
                        "Name": "PQR",
                        "Age": "32"
                    },
                    {
                        "Name": "MNO",
                        "Age": "26"
                    }
                ]
            }
        ]
    }
输入参数
propertyParentPath@body().sets
propertySubPathpeople
keyWithinCollectionPathName
newPropertyPathpeople
retainKey

输出 JSON:

    {
        "sets":[
            {
                "people":{
                    "XYZ":{
                        "Name": "XYZ",
                        "Age": "30"
                    },
                    "ABC":{
                        "Name": "ABC",
                        "Age": "23"
                    }
                }
            },
            {
                "people":{
                    "PQR":{
                        "Name": "PQR",
                        "Age": "32"
                    },
                    "MNO":{
                        "Name": "MNO",
                        "Age": "26"
                    }
                }
            }
        ]
    }

请注意,由于父 sets 是一个集合,因此转换将应用于每个子路径 people,键 Name 将保留,因为 retainKey 设置为 true。

开放源代码的连接器的示例

我们有一个将在开放源代码的连接器存储库中使用的此模板的实例。

连接器方案
Planner将引用数组转换为对象,其中 id 是“更新任务详细信息”操作请求中的键。

输入参数

名称 必需 Type 描述
Target object or collection path x-ms-apimTemplateParameter.propertyParentPath String 对象或集合的路径。
Property subpath x-ms-apimTemplateParameter.propertySubPath String 对象或集合中的属性子路径。
Path to object key within the array x-ms-apimTemplateParameter.keyWithinCollectionPath String 数组每个项中的键的子路径。
Path of the new property x-ms-apimTemplateParameter.newPropertyPath String 新属性的路径。
Retain key x-ms-apimTemplateParameter.retainKey 枚举 将键保留为属性。
- true
- false
Run policy on x-ms-apimTemplate-policySection 枚举 指定运行此策略的时间
- 请求:策略模板将在请求发送到后端 API 之前运行。
- 响应:策略将在收到后端 API 的响应后运行。
Operations x-ms-apimTemplate-operationName 数组 将对其应用策略的操作和触发器的列表。 如果未选择任何操作,该策略将应用于所有操作。