Share via


How to get maximum date from json data in logic app?

Question

Thursday, May 16, 2019 5:23 PM

I have below json output from my HTTP trigger 

{
  "main": {
    "data": [
      {
    "name":"aaa",
        "CurrentDate": "2019-05-11",
        "CurrentTime": "01:09:14"
      },
        {
       "name":"ccc",
        "CurrentDate": "2019-05-11",
        "CurrentTime": "01:11:14"
      },
    {
        "name":"ddd",
        "CurrentDate": "2019-05-11",
        "CurrentTime": "02:01:14"
      },
    {
        "name":"sss",
        "CurrentDate": "2019-05-11",
        "CurrentTime": "02:09:14"
      },
    {
       "name":"zzz",
        "CurrentDate": "2019-05-11",
        "CurrentTime": "02:19:14"
      }
    ]
  }
}

I have pass this data to for-each condition 

inside loop pass only data array[]which will traverse through records one by one.

I want to initialize one variable and save variable recentData = "CurrentDate+CurrentTime" but I want the latest one only in that variable.

So that I can save that value somewhere in storage and next time when logic app run it ignore all previous records and take only next records from `recentData` only.

 In above example this data is recent 

    {
       "name":"zzz",
        "CurrentDate": "2019-05-11",
        "CurrentTime": "02:19:14"
      }

So when next time new and old data comes together from HTTP trigger I can ignore all records and process only new records.

I tried to assign a new value to variable into for-each but unable to compare date+time with previous data.

How to achieve this in logic app , thanks in advance i'm new to logic app .

SE

All replies (7)

Friday, May 17, 2019 6:04 AM âś…Answered | 2 votes

First, you need to combine date and time together as you are sorting by datetime. you can do with the Select action and then use the javescript inline code to sort the array and return the biggest datetime.

Logic App Code

{
    "definition": {
        "$schema": "https://schema.management.azure.com/providers/Microsoft.Logic/schemas/2016-06-01/workflowdefinition.json#",
        "actions": {
            "Execute_JavaScript_Code": {
                "inputs": {
                    "code": "function sortByKey(array, key) {\r\n    return array.sort(function(a, b) {\r\n        var x = a[key]; var y = b[key];\r\n        return ((x < y) ? -1 : ((x > y) ? 1 : 0));\r\n    });\r\n}\r\n\r\nvar result=sortByKey(workflowContext.actions.Select.outputs.body,'datetime');\r\nreturn result[result.length-1];\r\n\r\n"
                },
                "runAfter": {
                    "Select": [
                        "Succeeded"
                    ]
                },
                "type": "JavaScriptCode"
            },
            "Response": {
                "inputs": {
                    "body": "@outputs('Execute_JavaScript_Code')?['body']",
                    "statusCode": 200
                },
                "kind": "Http",
                "runAfter": {
                    "Execute_JavaScript_Code": [
                        "Succeeded"
                    ]
                },
                "type": "Response"
            },
            "Select": {
                "inputs": {
                    "from": "@triggerBody()",
                    "select": {
                        "datetime": "@concat(item()?['CurrentDate'],' ',item()?['CurrentTime'])",
                        "name": "@item()?['name']"
                    }
                },
                "runAfter": {},
                "type": "Select"
            }
        },
        "contentVersion": "1.0.0.0",
        "outputs": {},
        "parameters": {},
        "triggers": {
            "manual": {
                "inputs": {
                    "schema": {}
                },
                "kind": "Http",
                "type": "Request"
            }
        }
    }
}

Input json:

[
      {
    "name":"aaa",
        "CurrentDate": "2019-05-11",
        "CurrentTime": "01:09:14"
      },
        {
       "name":"ccc",
        "CurrentDate": "2019-05-11",
        "CurrentTime": "01:11:14"
      },
    {
        "name":"ddd",
        "CurrentDate": "2019-05-11",
        "CurrentTime": "02:01:14"
      },
    {
        "name":"sss",
        "CurrentDate": "2019-05-11",
        "CurrentTime": "02:09:14"
      },
    {
       "name":"zzz",
        "CurrentDate": "2019-05-11",
        "CurrentTime": "02:19:14"
      }
    ]

Output:

{
    "datetime": "2019-05-11 02:19:14",
    "name": "zzz"
}

Thanks Sovit


Friday, May 17, 2019 10:29 AM

thanks you very much for detail answer but when i try to use Execute_JavaScript_Code

i got error - 

Failed to save logic app . The workflow must be associated with an integration account to use the workflow run action 'Execute_JavaScript_Code' of type 'JavaScriptCode'.

why needed integration account any clue?

SE


Saturday, May 18, 2019 10:42 PM | 1 vote

Create a free integration account and link your logic app to it. Javascript inline code needs an integration account:

/en-us/azure/logic-apps/logic-apps-enterprise-integration-create-integration-account

Thanks Sovit


Monday, May 20, 2019 2:18 PM

thanks you very much , is there any connector for key vault i want to save this value into key vault and use it next time..

SE


Tuesday, May 21, 2019 10:40 AM

thanks wokring fine but client do not want to use integration account :( 

can we do it using something like compose and filter array ? please guide thanks :)

SE


Tuesday, May 21, 2019 11:05 AM

HI Ashu,

you can use Azure function to do the same work if you don't want to use Integration account, and also there is azure Key Vault connector available in logic app.

Best Regards,
Suraj Revankar


Tuesday, May 21, 2019 11:06 AM

good alternative thank you :)

SE