簡體   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