JQ:将UNIX时间戳转换为Datetime

时间:2017-06-12 10:47:26

标签: jq iso8601

我在JQ1.5环境下使用实际Windows 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"。但只能解析错误。

2 个答案:

答案 0 :(得分:2)

使用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"
  }
]

答案 1 :(得分:0)

我在解析 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"
  }
]