![](/img/trans.png)
[英]JSON: convert epoch timestamp - jq: error (at <stdin>): Cannot index string with string “timestamp”
[英]How to convert an ISO timestamp field in JSON to epoch using jq
我有一組 JSON 消息,如下所示:
{
"type": "Point",
"time": "2021-04-01T01:19:21.243866342Z",
"value": 1,
"metric": "iterations",
"method": "",
"name": "",
"proto": "",
"status": "",
"tls_version": "",
"url": "",
"expected_response": "",
"group": "",
"scenario": "default",
"agent_hostname": "ip-13-3-3-33.ap-southeast-2.compute.internal",
"agent_ipaddress": "33.3.3.3",
"agent_casetype": "simplerequest",
"agent_casename": "test_case"
}
這是由應用程序生成並存儲在文件夾中的消息。 我想將時間字段從 ISO 轉換為紀元微秒。
我可以使用這個:.time_field |.[0:-9] | strptime("%Y-%m-%dT%H:%M:%S") | 時間
但這刪除了整個小數部分,只給了我第二個粒度。 這是趨勢數據,可能會在同一秒內發生一些事件。
我怎樣才能使用 jq 來實現呢?
謝謝
您對僅使用整數秒的時間函數是正確的。 這個答案使用字符串 function 來解析分數。
如果這是file.json
包含對象數組
[
{
"type": "Point",
"time": "2021-04-01T01:19:21.243866342Z"
},
{
"type": "Circle",
"time": "2020-01-01T00:00:00.01Z"
},
{
"type": "Line",
"time": "1970-01-01T01:00:00Z"
}
]
然后
jq '
def datestamp2epoch:
. | scan("(.+?)([.][0-9]+)?Z$")
| [(.[0] + "Z" | fromdateiso8601), (.[1] // 0 | tonumber)]
| add;
map(.time |= datestamp2epoch)
' file.json
[
{
"type": "Point",
"time": 1617243561.2438664
},
{
"type": "Circle",
"time": 1577836800.01
},
{
"type": "Line",
"time": 3600
}
]
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.