Try for free Book a demo

How to get property value from JSON message

Microsoft Azure

5 Mins Read

Recently, I had a scenario in which it was necessary to access the content of the source JSON message and extract a property that contains a date-time. That property was in the last object (a repeating object) for control purposes, and my first idea was to get it done with Azure Function and that’s it.

But then I was thinking, if I need to use Function for all these simple tasks, what’s the point? If I was doing a similar task in a BizTalk Server project, most of the time we use XPath and expressions to do it, so maybe there is a different way to do this inside Logic Apps.

The POC Scenario

To explain the requirement a bit better, let’s do a simple proof-of-concept. So, let’s imagine that we have the following JSON message:

{

“user”: “sandro.pereira@devscope.net”,

“post”: [

{

“dateTime”: “2020-01-20 12:30:00”,

“blog”: “https://www.turbo360.com/blog”,

“Topic”: “Logic Apps”

},

{

“dateTime”: “2020-01-22 23:00:00”,

“blog”: “https://blog.sandro-pereira.com/”,

“Topic”: “Logic Apps”

},

{

 “dateTime”: “2020-01-27 12:30:00”,

“blog”: “https://www.turbo360.com/blog”,

“Topic”: “Power Automate”

}

]

}

The important things (rules) to know are:

  • The message always contains one or more post objects
    • In the real scenario, we are controlling its existence
  • The message objects come sorted from oldest to most recent, so we don’t need to worry about ordering it

So, the goal of this POC is to get the dateTime property value of the last post object of the message – the one that is highlighted in bold and red above on the message sample.

Again, you can do it easily with a simple Function App, but the goal of this article is: Can we do this inside the Logic App designer with out-of-the-box functionalities without calling other Azure features (services)?

And the answer is, yes, luckily we can.

To exemplify, let’s build a simple Logic App that receives a JSON payload and return the last date-time in the response. To do that, we will use a Request – When a HTTP request is received trigger:

  • On the HTTP trigger, to be able to play easier with the JSON payload, you can click on the option Use a sample payload to generate schema and copy/paste the sample JSON above, to generate a body JSON schema and with that, the runtime will create proper tokens for the objects and properties

Logic App request

Now, for extracting the last dateTime property value we will be using:

  • A Variable – not really necessary in this POC, but in real cases maybe we need to use it in several places. So it will be better to reuse the variable value. This way, you don’t need to create or copy the formula that is a little more difficult and hard to maintain if you need to modify;
  • The use of the last workflow function, this function will return the last item from a specific collection
    • last(‘<collection>’)
    • In Microsoft documentation, you will find the following samples:
      • last(‘abcd’)
      • last(createArray(0, 1, 2, 3))
    • That will return the following results:
      • First example: “d”
      • Second example: 3

Building the expression

In our case, we need to get the array of post objects from the source message, and you can do it with the following expression:

@triggerBody()?[‘post’]

Now we need to position ourselves in the last occurrence of this object or record and to do that we need to use the workflow function last, that will make the expression like this:

@{last(triggerBody()?[‘post’])

Note: Because we are combining multiple expressions, we use @{} that wrap everything inside a string – String interpolation.

However, this last sentence gives us the complete structure of the last post object, and we need/want only the dateTime property value. To get that, we should use the following expression:

@{last(triggerBody()?[‘post’])?[‘dateTime’]}

To do that we need:

  • On the Logic App designer, add the next step by clicking the +New step button
  • On the Choose an action window, enter Variables and select the action Initialize variable
  • On the Variable action configuration:
    • On the Name property, type “LastDate”
    • On the Type property, specify the type as “String”
    • On the Value property past the following expression
      • @{last(triggerBody()?[‘post’])?[‘dateTime’]}

Logic App workflow

Note: when you paste the above expression, the Logic App designer will render it to what you see in the picture. Basically, it is the same as when you build the expression using the Expression editor.

get value from json

The only difference is that while using the Expression editor, we don’t use the String interpolation on the expression, aka @{}.

To finalize:

  • On the Logic App designer, add the next step by clicking the +New step button
  • On the Choose an action window, enter Request – Response action
  • On the Response action configuration:
    • Leave the default values
    • And on the body place the following payload

{

“LastDate”: ” @{variables(‘LastDate’)}”

}

JSON message

Now, if we try using a tool like Postman, to try our Logic Apps we will see that we will get the last dateTime property value back on the response of our request.

get value from json

Quick, simple and practical.

Stay tuned for new Azure Logic Apps tips and tricks!

This article was published on Feb 4, 2020.

Related Articles