【问题标题】:JQ: Transform UNIX Timestamp to DatetimeJQ:将 UNIX 时间戳转换为日期时间
【发布时间】:2017-06-12 10:47:25
【问题描述】:

我实际上在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”。但只得到解析错误。

【问题讨论】:

  • 你应该发布带​​有关键数据的输入 json 片段
  • 嗨 @RomanPerekhrest 添加了数据
  • 您的代码和示例 json 不匹配。请考虑根据MCVE 简化您的代码和示例

标签: jq iso8601


【解决方案1】:

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

【讨论】:

  • 对于任何寻找逆向的人,它是fromdateiso8601
  • 对于任何需要进行类似翻译但时间以毫秒为单位的人,过滤器(|= 之后的片段)可以是这样的:(. / 1000 | round | todateiso8601)
【解决方案2】:

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

【讨论】:

    猜你喜欢
    • 2020-10-19
    • 2015-02-09
    • 1970-01-01
    • 2020-12-23
    • 2016-04-24
    • 2012-07-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多