Azure Logic Apps–Parsing JSON message from service bus
What I want: When the logic app trigger receives a JSON formatted message from Azure Service Bus topic, I want to send a notification to the “email” field. My sample message structure looks like this:
What happens: Because a message received on service bus doesn’t have a predefined format – it could be JSON, XML, or anything else – so Logic Apps doesn’t know the structure of the message. So in the designer, it looks like:
Which is great, but it just dumps out the entire object, and not the email field that I need.
How to fix it: Fortunately the fix is pretty easy, basically you need
Select the Content output (above), you are going to edit this value.
Switch over to ‘Code view’ and manually type the expression (below).
If you haven't used it before, code view can be found in the toolbar:
Once you are in the code view, scroll down to the connector you are interested in. You will see the expression for the trigger body. This is the entire message received from the trigger, basically.
You need to modify this to parse the entire message using the ‘json’ function, then you can access it’s typed fields.
If you have ever used JSON.parse (or any object deserialization in pretty much any language for that matter) this concept should be familiar to you. When I was done I ended up with:
I’ve broken the entire segment into two parts, a) parses the content and b) accesses the ‘email’ field of the parsed JSON object.
Hope this helps someone!
Update: if you are seeing an error when trying to parse see my new blog post Azure Logic Apps-The template language function 'json' parameter is not valid.