Adding a JSON schema to your Logic App – Generate, Copy & Paste
Although I see many Logic Apps triggered by events like schedules or external events like a Twitter message, it’s also possible to trigger it by an HTTP call. A nice feature, but it would be even better if we were able to get the definition in the Logic App as well, so we know what properties we can use in our flow. And you know what? Yes we can!
Let’s start by creating an easy Logic App for this. It receives an HTTP message and uses data from the input to generate the output. After some really hard work, I ended up with this:
Well, actually, that wasn’t that hard right?
Before the last release you really needed to dig to find the URL you needed to call to manually trigger your Logic App. Now you will be able to see the URL in the designer after you save the Logic App.
Let’s save this and get our JSON together.
JSON message definition
I will be sending the following request:
{ "name": { "firstname": "Rob", "lastname": "Fox" }, "email": [ { "home": "rob@robfox.io" }, { "work": "work@robfox.io" } ] }
All we need to do now is get the schema. Uhm. How it works can be found here: http://json-schema.org/. But let’s not go through this now, just generate it over here: http://jsonschema.net/
At jsonschema.net you can paste in your JSON message and simply generate the definition. Maybe you need to fiddle around and adjust some settings. But after that your schema looks something like this:
{ "$schema": "http://json-schema.org/draft-04/schema#", "id": "http://jsonschema.net", "type": "object", "properties": { "name": { "id": "http://jsonschema.net/name", "type": "object", "properties": { "firstname": { "id": "http://jsonschema.net/name/firstname", "type": "string" }, "lastname": { "id": "http://jsonschema.net/name/lastname", "type": "string" } }, "required": [ "firstname", "lastname" ] }, "email": { "id": "http://jsonschema.net/email", "type": "array", "items": [ { "id": "http://jsonschema.net/email/0", "type": "object", "properties": { "home": { "id": "http://jsonschema.net/email/0/home", "type": "string" } } }, { "id": "http://jsonschema.net/email/1", "type": "object", "properties": { "work": { "id": "http://jsonschema.net/email/1/work", "type": "string" } } } ] } }, "required": [ "name", "email" ] }
Now we need to get our schema into the Logic App. The only way to do this now, is by pasting it into the code view (remember, actually developing). If you open the Code View (</>) of your Logic App, you’ll notice that there is not that much to it. The code will be similar to this:
{ "$schema": "https://schema.management.azure.com/providers/Microsoft.Logic/schemas/2015-08-01-preview/workflowdefinition.json#", "actions": { "Response": { "conditions": [], "inputs": { "statusCode": 200 }, "type": "Response" } }, "contentVersion": "1.0.0.0", "outputs": {}, "parameters": {}, "triggers": { "manual": { "inputs": { "schema": { "properties": {}, "required": [], "type": "object" } }, "type": "Manual" } } }
If you have a look at line 18, you’ll notice the schema property, located within our triggers/inputs. That’s were the JSON Schema needs to go. You can just paste the entire generated schema. The Logic App will automatically get rid of overhead after you switch to designer view and back.
So if we look at the snippet after the triggers key, this should be the result:
"triggers": { "manual": { "inputs": { "schema": { "properties": { "email": { "id": "http://jsonschema.net/email", "items": [ { "id": "http://jsonschema.net/email/0", "properties": { "home": { "id": "http://jsonschema.net/email/0/home", "type": "string" } }, "type": "object" },
So, that’s that! The schema is in the Logic App and we can now see if the magic is actually working. Go back into your Designer view and open the Response card by clicking on it. Lo and behold! Our properties are actually showing up in the Designer. And we can now use them in our response body.
In Code view, you’ll actually see what the generated code is for the response:
"Response": { "conditions": [], "inputs": { "body": "Hello @{triggerBody()['name']['firstname']}! Or should I say Mr. @{triggerBody()['name']['lastname']}?", "statusCode": 200 }, "type": "Response" }
Let’s save this and see if it works! I normally use Postman for this, but there are other tools around that work just as well.
There’s not really much more to it. It’s that simple to get your API definition in your Logic App. You can now use Swagger to get the definition out in the open, or you may choose to use a Function App or wrap it in an API App, which gives you a lot more control over things like security.
do you have any neat json color schemes