json - 如何在逻辑应用程序中将字符串解析为 JSON?

标签 json azure-logic-apps json-deserialization

我在下面有从外部实体收到的 JSON。如您所见 requestbody参数显示为 string即使它是 JSON。那么我该如何转义它以便我可以正确地解析它下游呢?

{
  "emailaddress": "174181@mycomp.com",
  "requestbody": "{\"Id\":\"57518139-687c-4223-b08b-342f4ff426ca\",\"Properties\":{\"PrincipalId\":\"d701e7aa-5a0a-4c4a-81be-4c4b7a3967ce\",\"RoleDefinitionId\":\"/subscriptions/64ba3e4c-45e3-4d55-8132-6731cf25547f/providers/Microsoft.Authorization/roleDefinitions/8e3af657-a8ff-443c-a75c-2fe8c4bcb635\",\"Scope\":\"/subscriptions/64ba3e4c-45e3-4d55-8132-6731cf25547f\"}}"
}

最佳答案

  • 使用如下所示的解析 JSON 操作:

  • 内容:
    {
      "emailaddress": "174181@mycomp.com",
      "requestbody": "{\"Id\":\"57518139-687c-4223-b08b-342f4ff426ca\",\"Properties\":{\"PrincipalId\":\"d701e7aa-5a0a-4c4a-81be-4c4b7a3967ce\",\"RoleDefinitionId\":\"/subscriptions/64ba3e4c-45e3-4d55-8132-6731cf25547f/providers/Microsoft.Authorization/roleDefinitions/8e3af657-a8ff-443c-a75c-2fe8c4bcb635\",\"Scope\":\"/subscriptions/64ba3e4c-45e3-4d55-8132-6731cf25547f\"}}"
    }
    

    架构
    {
        "$schema": "http://json-schema.org/draft-04/schema#",
        "properties": {
            "emailaddress": {
                "type": "string"
            },
            "requestbody": {
                "type": "string"
            }
        },
        "required": [
            "emailaddress",
            "requestbody"
        ],
        "type": "object"
    }
    

    ParseJson
  • 初始化变量

  • -Name = Variable Name
    -Type = Object
    -Value = json(body('Parse_JSON')['requestbody'])
    

    enter image description here
  • 现在您可以提取 Json 字符串的属性,如下所示:
  • variables('jsonobj')?['Properties']
    

    enter image description here

    我的示例的完整代码 View :
    {
        "definition": {
            "$schema": "https://schema.management.azure.com/providers/Microsoft.Logic/schemas/2016-06-01/workflowdefinition.json#",
            "actions": {
                "Initialize_variable": {
                    "inputs": {
                        "variables": [
                            {
                                "name": "jsonobj",
                                "type": "Object",
                                "value": "@json(body('Parse_JSON')['requestbody'])"
                            }
                        ]
                    },
                    "runAfter": {
                        "Parse_JSON": [
                            "Succeeded"
                        ]
                    },
                    "type": "InitializeVariable"
                },
                "Parse_JSON": {
                    "inputs": {
                        "content": {
                            "emailaddress": "174181@mycomp.com",
                            "requestbody": "{\"Id\":\"57518139-687c-4223-b08b-342f4ff426ca\",\"Properties\":{\"PrincipalId\":\"d701e7aa-5a0a-4c4a-81be-4c4b7a3967ce\",\"RoleDefinitionId\":\"/subscriptions/64ba3e4c-45e3-4d55-8132-6731cf25547f/providers/Microsoft.Authorization/roleDefinitions/8e3af657-a8ff-443c-a75c-2fe8c4bcb635\",\"Scope\":\"/subscriptions/64ba3e4c-45e3-4d55-8132-6731cf25547f\"}}"
                        },
                        "schema": {
                            "$schema": "http://json-schema.org/draft-04/schema#",
                            "properties": {
                                "emailaddress": {
                                    "type": "string"
                                },
                                "requestbody": {
                                    "type": "string"
                                }
                            },
                            "required": [
                                "emailaddress",
                                "requestbody"
                            ],
                            "type": "object"
                        }
                    },
                    "runAfter": {},
                    "type": "ParseJson"
                },
                "Response": {
                    "inputs": {
                        "body": "@variables('jsonobj')?['Properties']",
                        "statusCode": 200
                    },
                    "kind": "Http",
                    "runAfter": {
                        "Initialize_variable": [
                            "Succeeded"
                        ]
                    },
                    "type": "Response"
                }
            },
            "contentVersion": "1.0.0.0",
            "outputs": {},
            "parameters": {},
            "triggers": {
                "manual": {
                    "inputs": {
                        "schema": {}
                    },
                    "kind": "Http",
                    "type": "Request"
                }
            }
        }
    }
    

    关于json - 如何在逻辑应用程序中将字符串解析为 JSON?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54798639/

    相关文章:

    ios - dispacth_sync与dispatch_async:iOS JSON解析

    xml - 逻辑应用 XML 管理

    azure - 使用逻辑应用进行 ServiceBus 死信队列清理

    java - 如何在 Java 中使用 WSDL 文件的序列化器?

    c# - 将 JSON 解析为 JToken 时如何将所有键更改为小写

    ios - 捆绑和加载要在 iOS 应用程序中随机读取的对象

    php - 解析由 php json_encode 创建的 JSON 数据

    java - 解析 C# 中以 JSON 发送的 DateTime 类型

    azure - Azure 调度程序和 Azure 逻辑应用程序的部署槽

    java - 如何从 START_OBJECT token 中反序列化 java.lang.String 实例