gpt4 book ai didi

JQ:将 UNIX 时间戳转换为日期时间

转载 作者:行者123 更新时间:2023-12-01 12:20:36 25 4
gpt4 key购买 nike

我实际使用 JQ1.5Windows 10环境转换几个 json 文件以导入到 MS SQL数据库。部分数据格式为UNIX timestamp我需要将这些数据转换为 ISO 8601 格式。

以下命令我实际用于数据转换:

jq '
[
{ nid, title, nights, zone: .zones[0].title} +
(.sails[] | { sails_nid: .nid, arrival, departure } ) +
(.sails[].cabins[] |
{ cabintype: .cabinType.kindName,
cabinid: .cabinType.nid,
catalogPrice,
discountPrice,
discountPercentage,
currency
}
)
]
' C:\Import\dreamlines_details.json > C:\Import\import_sails.json

到达和离开是 Unix 时间格式的数据。

数据:

[
{
"nid": 434508,
"title": "Die schönsten Orte unserer Welt",
"nights": 121,
"zone": "Weltreise",
"sails_nid": 434516,
"arrival": 1525644000,
"departure": 1515193200,
"cabintype": "Innenkabine",
"cabinid": 379723,
"catalogPrice": 17879,
"discountPrice": 9519,
"discountPercentage": 0.4675876726886291,
"currency": "EUR"
},
{
"nid": 434508,
"title": "Die schönsten Orte unserer Welt",
"nights": 121,
"zone": "Weltreise",
"sails_nid": 434516,
"arrival": 1525644000,
"departure": 1515193200,
"cabintype": "Innenkabine",
"cabinid": 379730,
"catalogPrice": 18599,
"discountPrice": 10239,
"discountPercentage": 0.44948653153395346,
"currency": "EUR"
}
]

我尝试了内置运算符“todate”和“strftime”。但只得到解析错误。

最佳答案

使用 todateiso8601功能:

jq '.[].arrival |= todateiso8601 | .[].departure |= todateiso8601' C:\Import\import_sails.json

输出(对于您的输入片段):

[
{
"nid": 434508,
"title": "Die schönsten Orte unserer Welt",
"nights": 121,
"zone": "Weltreise",
"sails_nid": 434516,
"arrival": "2018-05-06T22:00:00Z",
"departure": "2018-01-05T23:00:00Z",
"cabintype": "Innenkabine",
"cabinid": 379723,
"catalogPrice": 17879,
"discountPrice": 9519,
"discountPercentage": 0.4675876726886291,
"currency": "EUR"
},
{
"nid": 434508,
"title": "Die schönsten Orte unserer Welt",
"nights": 121,
"zone": "Weltreise",
"sails_nid": 434516,
"arrival": "2018-05-06T22:00:00Z",
"departure": "2018-01-05T23:00:00Z",
"cabintype": "Innenkabine",
"cabinid": 379730,
"catalogPrice": 18599,
"discountPrice": 10239,
"discountPercentage": 0.44948653153395346,
"currency": "EUR"
}
]

关于JQ:将 UNIX 时间戳转换为日期时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44497533/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com