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

JQ: Transform UNIX Timestamp to Datetime

我实际上在 Windows 10 环境下使用 JQ1.5 转换几个 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"
  }
]

我在解析 Perforce 的输出时遇到了类似的问题(使用 -Mj 选项), 但纪元时间是 字符串 ,而不是数字。

$ p4 -z tag -Mj labels -e "test_build" | jq '.'
{
  "Access": "1581356898",
  "Description": "Created by p4build.\n",
  "Options": "unlocked noautoreload",
  "Owner": "p4build",
  "Update": "1580936739",
  "label": "test_build"
}

tonumber 添加到过滤器中修复它:

$ p4 -z tag -Mj labels -e "test_build" > test.json
$ jq -s '.[].Access |= (tonumber | todateiso8601) | .[].Update |= (tonumber | todateiso8601)' test.json
[
  {
    "Access": "2020-02-10T17:48:18Z",
    "Description": "Created by p4build.\n",
    "Options": "unlocked noautoreload",
    "Owner": "p4build",
    "Update": "2020-02-05T21:05:39Z",
    "label": "test_build"
  }
]