Using and abusing the Compose JSON action in Logic Apps
So that Compose JSON action in Azure Logic Apps is kinda under-documented. At least, I had to figure it out all by myself, but when you get the hang of it, it is actually quite simple.
The action accepts JSON and only JSON. So don’t you dare to enter invalid JSON. You will be punished by a Logic App that you can’t save.
So the action is meant to compose a JSON message. That’s not too hard, but let’s be honest. It could have done with a smaller and different font, bigger box and maybe some colors, line numbers and something that doesn’t break if you try to copy and paste… But hey. My background is BizTalk, so what am I complaining about? I am happy with my resizable schema selector in BizTalk 2016 😉
Use the shape by entering a JSON message like this:
Using dynamic content in your JSON message
The next step is to use dynamic content in your JSON message. Luckily that can be achieved fairly easy, but don’t make any mistakes. The designer is unforgiving. You may need to fiddle around a little bit, but collections can also be added (e.g. “sites”). The dynamic content in the message below is coming from the HTTP Request that was received by the Logic App.
Abuse the action to assign variables
Something that is missing in Logic Apps is the ability to assign variables. Or – to quote Jeremy Clarkson – is it…?!
The Compose JSON action can be abused to assign variables in some way at least. So if you need to store some stuff, you could abuse this action for that. Let’s assume the following Logic App setup for testing purposes. It starts with an HTTP Request, then the Compose JSON action and to complete the loop, we are sending an HTTP Response.
In the Compose action, put some JSON with two variables in this case. A first name and a last name. You can also put some dynamic content in here like I demonstrated above.
Since we are just composing the JSON and not parsing it, there’s no – let’s call it – IntelliSense: the dynamic content is not visible. And we need to fill out the expression ourselves in the Response action. After filling out the expression and testing the Logic App in Postman, it will magically return “Rob”, which just happens to be my first name. Neat!
To access the variables with some elegance and IntelliSense, you’ll need to parse the JSON message containing the variables. You can easily generate a schema at jsonschema.net. Just copy and paste the JSON and hit the Generate button to generate the schema. Add the Parse JSON action just below the Compose Action, then copy and paste the schema in the action. The Outputs of Init vars should be parsed.
If you’d open the Response action now and have a look at the dynamic content, you’ll notice that the firstname and lastname are showing up there now. Now you can use this dynamic content to return the exact same thing we just returned, only now your variables are accessible by the GUI. The impact? It’ll cost you an action each time the Logic Apps executes.
And with some trickery and a little knowledge of the Logic Apps workflow definition language, you can get into some more advanced scenarios, like saving XML in a variable and then returning that.
I could go on with some other scenarios as well, but I encourage you to try some stuff yourself as well! You can achieve quite a lot with some knowledge of the workflow definition language.
But anyway, to sum it up. Abusing the Compose JSON action gives us the option of using variables in Logic Apps. Kinda… But I still agree that there should be some decent mechanism for this.