繁体   English   中英

将 JSON 数组解析为 Azure 逻辑应用程序中的各个变量

[英]Parsing JSON array to individual variables in Azure logic apps

我正在尝试编写一个逻辑应用程序来解析 Json Object 和更新 salesforce 记录。 我对 Salesforce 和 Azure 逻辑应用程序都很陌生,所以我想弄清楚这一点。 下面是我的 Json 文件

{
  "ContactId": null,
  "Email": "asong@uog.com",
  "IsInternalUpdate": false,
  "Preferences": [
    {
      "PrefCode": "EmailOptIn",
      "CurrentValue": "Yes",
      "Locale": "en-US"
    },
    {
      "PrefCode": "MobilePhone",
      "CurrentValue": "1234567890",
      "Locale": "en-US"
    },
    {
      "PrefCode": "SMSOptIn",
      "CurrentValue": "Yes",
      "Locale": "en-US"
    },
    {
      "PrefCode": "ProductTrends",
      "CurrentValue": "ProductTrends,OffersPromotions",
      "Locale": "en-US"
    },
  ]
}

基于 email 值,我需要更新 Salesforce 中的自定义 object。从首选项数组中,Prefcode 值映射到 Salesforce 中的一个字段,Current 值映射到字段值。 即下面的代码片段翻译为将 Salesforce 中的 EmailOptIn 字段的值设置为“是”

    {
      "PrefCode": "EmailOptIn",
      "CurrentValue": "Yes",
      "Locale": "en-US"
    }

到目前为止,我能够传递硬编码值并从逻辑应用程序成功更新 salesforce 记录。

我正在尝试为每个字段设置单独的变量,以便我可以将其直接传递给 salesforce。我遇到了两个问题

  1. 捕获字段值映射的最佳方法是什么?
  2. 我有几个字段允许多选 select,如何设置多选值。 下面是一个例子
{
      "PrefCode": "ProductTrends",
      "CurrentValue": "ProductTrends,OffersPromotions",
      "Locale": "en-US"
}

以下是我的逻辑应用程序结构

1个

2个

  1. 捕获字段值映射的最佳方法是什么?

我可以通过构建具有所需详细信息的 JSON 并再次使用解析 JSON 步骤来实现您的要求。 以下是为我工作的逻辑应用程序的流程。

在此处输入图像描述

在此处输入图像描述

  1. 我有几个字段允许多选 select,如何设置多选值。 下面是一个例子

我已将ProductTrends转换为数组并使用其索引检索每个项目,如下所示。

array(split(body('Parse_JSON_2')?['ProductTrends'],','))[0]
array(split(body('Parse_JSON_2')?['ProductTrends'],','))[1]

或者,如果可以使多个值的目标端工作,您可以将数组或字符串直接发送到 salesforce。

下面是我的逻辑应用程序的完整 JSON。

{
    "definition": {
        "$schema": "https://schema.management.azure.com/providers/Microsoft.Logic/schemas/2016-06-01/workflowdefinition.json#",
        "actions": {
            "Compose": {
                "inputs": {
                    "ContactId": null,
                    "Email": "asong@uog.com",
                    "IsInternalUpdate": false,
                    "Preferences": [
                        {
                            "CurrentValue": "Yes",
                            "Locale": "en-US",
                            "PrefCode": "EmailOptIn"
                        },
                        {
                            "CurrentValue": "1234567890",
                            "Locale": "en-US",
                            "PrefCode": "MobilePhone"
                        },
                        {
                            "CurrentValue": "Yes",
                            "Locale": "en-US",
                            "PrefCode": "SMSOptIn"
                        },
                        {
                            "CurrentValue": "ProductTrends,OffersPromotions",
                            "Locale": "en-US",
                            "PrefCode": "ProductTrends"
                        }
                    ]
                },
                "runAfter": {},
                "type": "Compose"
            },
            "Compose_2": {
                "inputs": "@array(split(body('Parse_JSON_2')?['ProductTrends'],','))[0]",
                "runAfter": {
                    "Parse_JSON_2": [
                        "Succeeded"
                    ]
                },
                "type": "Compose"
            },
            "For_each": {
                "actions": {
                    "Append_to_string_variable": {
                        "inputs": {
                            "name": "Preferences",
                            "value": "\"@{items('For_each')?['PrefCode']}\":\"@{items('For_each')?['CurrentValue']}\",\n"
                        },
                        "runAfter": {},
                        "type": "AppendToStringVariable"
                    }
                },
                "foreach": "@body('Parse_JSON')?['Preferences']",
                "runAfter": {
                    "Initialize_variable": [
                        "Succeeded"
                    ]
                },
                "type": "Foreach"
            },
            "Initialize_variable": {
                "inputs": {
                    "variables": [
                        {
                            "name": "Preferences",
                            "type": "string"
                        }
                    ]
                },
                "runAfter": {
                    "Parse_JSON": [
                        "Succeeded"
                    ]
                },
                "type": "InitializeVariable"
            },
            "Parse_JSON": {
                "inputs": {
                    "content": "@outputs('Compose')",
                    "schema": {
                        "properties": {
                            "ContactId": {},
                            "Email": {
                                "type": "string"
                            },
                            "IsInternalUpdate": {
                                "type": "boolean"
                            },
                            "Preferences": {
                                "items": {
                                    "properties": {
                                        "CurrentValue": {
                                            "type": "string"
                                        },
                                        "Locale": {
                                            "type": "string"
                                        },
                                        "PrefCode": {
                                            "type": "string"
                                        }
                                    },
                                    "required": [
                                        "PrefCode",
                                        "CurrentValue",
                                        "Locale"
                                    ],
                                    "type": "object"
                                },
                                "type": "array"
                            }
                        },
                        "type": "object"
                    }
                },
                "runAfter": {
                    "Compose": [
                        "Succeeded"
                    ]
                },
                "type": "ParseJson"
            },
            "Parse_JSON_2": {
                "inputs": {
                    "content": "@concat('{',slice(variables('Preferences'),0,lastIndexOf(variables('Preferences'),',')),'}')",
                    "schema": {
                        "properties": {
                            "EmailOptIn": {
                                "type": "string"
                            },
                            "MobilePhone": {
                                "type": "string"
                            },
                            "ProductTrends": {
                                "type": "string"
                            },
                            "SMSOptIn": {
                                "type": "string"
                            }
                        },
                        "type": "object"
                    }
                },
                "runAfter": {
                    "For_each": [
                        "Succeeded"
                    ]
                },
                "type": "ParseJson"
            }
        },
        "contentVersion": "1.0.0.0",
        "outputs": {},
        "parameters": {},
        "triggers": {
            "manual": {
                "inputs": {
                    "schema": {}
                },
                "kind": "Http",
                "type": "Request"
            }
        }
    },
    "parameters": {}
}

按照上述过程后,我可以单独获取偏好值。

在此处输入图像描述

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM