[英]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.我正在开发通过 HTTP 调用触发并返回 JSON 响应的 Azure 逻辑应用程序。 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.
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"
.我遇到的问题是 web API 以
"/Date(1616371200000)/"
格式返回日期,我需要日期格式看起来像"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).我没有看到任何可以按原样使用或转换 Epoch 时间戳的内置逻辑应用程序 function (除非我遗漏了一些东西)。
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(...)/"
:如果源日期未使用
"/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.请注意,不能选择使用Liquid JSON-to-JSON 模板。 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.
我正在使用它,发现此操作显然必须在使用前进行 JIT 编译,这导致我的逻辑应用程序在长时间不活动后被调用时超时。
The following expression seems to be working and returns a timestamp in UTC.以下表达式似乎正在工作并返回 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.请注意,
substring()
function 仅使用 10 而不是 13 的长度。我有意从 Epoch 时间戳中剪掉毫秒,因为addToTime()
function 只处理秒。
{
"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:另外,如果源 JSON 中的时间戳值为
null
,请执行以下操作:
{
"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?您能否将 JSON 中的值“/Date(1616371200000)/”转换为变量? 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.然后使用 addToTime 中的变量function 。
Result:结果:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.