Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do parse string as JSON in Logic App?

I have JSON below which I receive from external entity. As you can see requestbody parameter is appearing as string even though it's JSON. So how do I unescape it so I can correctly parse it downstream?

{
  "emailaddress": "[email protected]",
  "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\"}}"
}
like image 393
Gregory Suvalian Avatar asked Mar 05 '23 11:03

Gregory Suvalian


1 Answers

  1. Use a Parse JSON Action as shown below:

Content:

{
  "emailaddress": "[email protected]",
  "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"
}

ParseJson

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

enter image description here

  1. Now you can extract the properties of your Json string as shown below:
variables('jsonobj')?['Properties']

enter image description here

Full Code view of my sample:

{
    "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": "[email protected]",
                        "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"
            }
        }
    }
}
like image 135
Ketan Avatar answered Mar 15 '23 01:03

Ketan