繁体   English   中英

Azure 逻辑应用程序 - 将 JSON 纪元时间戳转换为 DateTime 字符串

[英]Azure Logic Apps - Convert JSON Epoch Timestamp to DateTime String

我正在开发通过 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.

我遇到的问题是 web API 以"/Date(1616371200000)/"格式返回日期,我需要日期格式看起来像"2021-03-32T19:00:00Z" 我没有看到任何可以按原样使用或转换 Epoch 时间戳的内置逻辑应用程序 function (除非我遗漏了一些东西)。

澄清...

源数据:

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

所需数据:

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

如果源日期未使用"/Date(...)/"包装,则以下解决方案理论上可行:

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

在转换之前从时间戳中解析该文本会导致一个非常难看的表达式。 有没有更好的方法来转换我不知道的时间戳?

请注意,不能选择使用Liquid JSON-to-JSON 模板 我正在使用它,发现此操作显然必须在使用前进行 JIT 编译,这导致我的逻辑应用程序在长时间不活动后被调用时超时。

以下表达式似乎正在工作并返回 UTC 时间戳。 请注意, 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')"
}

另外,如果源 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'))"
}

很可能是我写过的最丑陋的代码。

您能否将 JSON 中的值“/Date(1616371200000)/”转换为变量? 如果是这样,一个小的字符串操作就可以了:

在此处输入图像描述

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

然后使用 addToTime 中的变量function

结果:

在此处输入图像描述

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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