代码之家  ›  专栏  ›  技术社区  ›  Gregory Suvalian

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

  •  0
  • Gregory Suvalian  · 技术社区  · 5 年前

    我有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\"}}"
    }
    
    0 回复  |  直到 5 年前
        1
  •  2
  •   Ketan    5 年前
    1. 使用Parse 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

    1. 初始化变量
    -Name = Variable Name
    -Type = Object
    -Value = json(body('Parse_JSON')['requestbody'])
    

    enter image description here

    1. 现在可以提取Json字符串的属性,如下所示:
    variables('jsonobj')?['Properties']
    

    enter image description here

    我的示例的完整代码视图:

    {
        "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"
                }
            }
        }
    }
    
        2
  •  1
  •   Josh Williams    5 年前

    最简单的方法是使用以下表达式:

    @json(outputs('Mock_example_data').requestbody)
    

    下面是一个使用 Compose 模拟数据和其他 作曲 作为概念的简单证明。

    enter image description here