简体   繁体   中英

Azure Logic Apps - Convert JSON Epoch Timestamp to DateTime String

I am working on an Azure Logic App that is triggered via an HTTP call and returns a JSON response. Internally, the logic app retrieves JSON data from a web API and then converts the response JSON data to a format that is acceptable to the calling client of the logic app.

The problem I'm having is that the web API is returning dates in the format "/Date(1616371200000)/" and I need the date format to look like "2021-03-32T19:00:00Z" . I don't see any built-in logic app function that can work with or convert the Epoch timestamp as-is (unless I'm missing something).

To clarify...

Source Data:

{
    "some_silly_date": "/Date(1616371200000)/"
}

Desired Data:

{
    "some_silly_date": "2021-03-32T19:00:00Z"
}

The following solution would theoretically work if the source date wasn't wrapped with "/Date(...)/" :

"@addToTime('1970-01-01T00:00:00Z', 1616371200000, 'Second')"

Parsing that text off the timestamp before converting it would lead to a really ugly expression. Is there a better way to convert the timestamp that I'm not aware of?

Note that using the Liquid JSON-to-JSON templates is not an option. I was using that and found this action apparently has to JIT compile before use which is causing my logic app to time-out when being called after a long period of inactivity.

The following expression seems to be working and returns a timestamp in UTC. Note that the substring() function is only using a length of 10 instead of 13. I'm intentionally trimming-off the milliseconds from the Epoch timestamp because the addToTime() function only handles seconds.

{
   "some_silly_date": "@addToTime('1970-01-01T00:00:00Z', int(substring(item()?['some_silly_date'],6,10)), 'Second')"
}

As an added bonus, if the timestamp value is null in the source JSON, do the following:

{
   "some_silly_date": "@if(equals(item()?['some_silly_date'], null), null, addToTime('1970-01-01T00:00:00Z', int(substring(item()?['some_silly_date'],6,10)), 'Second'))"
}

Quite possibly the ugliest code I've ever written.

Can you get the value "/Date(1616371200000)/" from the JSON into a variable? If so, a little string manipulation would do the trick:

在此处输入图像描述

int(replace(replace(variables('data_in'),'/Date(',''),')/',''))

Then use the variable in the addToTime function.

Result:

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM