如何在Logic App中将字符串解析为JSON?

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\"}}"
}
2个回答

6
使用以下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

现在,您可以按照下面所示提取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"
            }
        }
    }
}

1
最简单的方法是使用这个表达式:

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

以下是一个示例,使用Compose操作来模拟您的数据,并使用另一个Compose操作作为简单的概念验证。

enter image description here


它是一个数组吗? - Neo
1
如果json()函数无法正常工作,您可以将输入包装在array()工作流函数中。 - Josh Williams

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接