重新格式化从unix到iso的时间戳

时间:2016-04-28 13:39:21

标签: unix-timestamp jq

我有Json输入数据,我需要将时间戳从unix-time重新格式化为ISO 8601(之后处理文件)。 我尝试使用以下方法执行此操作: <input.json jq .[2].timestamp |= jq todate >output.json 这是以正确的方式重新格式化时间戳,但是如何将重新格式化的时间戳重新放回原始文件中?我的目标是获取原始文件及其所有信息,但重新格式化时间戳。

它可以正常运行,因为我想在https://jqplay.org/处使用它,但不能在命令行中使用它。 感谢您的帮助!

示例输入:

[
{
"channelId": 9088,
"errorCode": 0,
"value": 0,
"timestamp": 1460258309
},
{
"channelId": 10087,
"errorCode": 0,
"value": 1000,
"timestamp": 1460258294
},
{
"channelId": 10086,
"errorCode": 0,
"value": 90,
"timestamp": 1460258294
},
{
"errorCode": 0,
"errorLine": ""
 }
]

通缉输出:

[
{
"channelId": 9088,
"errorCode": 0,
"value": 0,
"timestamp": 2016-04-10T03:18:14Z
},
{
"channelId": 10087,
"errorCode": 0,
"value": 1000,
"timestamp": 2016-04-10T03:18:14Z
},
{
"channelId": 10086,
"errorCode": 0,
"value": 90,
"timestamp": 2016-04-10T03:18:14Z
},
{
"errorCode": 0,
"errorLine": ""
 }
]

2 个答案:

答案 0 :(得分:2)

输入您的信息:

 <input.json jq 'map(if .timestamp then .timestamp |= todate else . end)'  

输出是:

[
  {
    "channelId": 9088,
    "errorCode": 0,
    "value": 0,
    "timestamp": "2016-04-10T03:18:29Z"
  },
  {
    "channelId": 10087,
    "errorCode": 0,
    "value": 1000,
    "timestamp": "2016-04-10T03:18:14Z"
  },
  {
    "channelId": 10086,
    "errorCode": 0,
    "value": 90,
    "timestamp": "2016-04-10T03:18:14Z"
  },
  {
    "errorCode": 0,
    "errorLine": ""
  }
]

答案 1 :(得分:0)

如果您想要转换“时间戳”的每一次出现(无论它出现在何处),那么如果您的jq有walk/1,您可以使用以下所示的过滤器:

jq -n '[{timestamp: (24*60*60)}] | walk(if type == "object" and .timestamp then .timestamp |= todate else . end)'
[
  {
    "timestamp": "1970-01-02T00:00:00Z"
  }
]

如果您的jq没有walk/1,那么您可以从https://github.com/stedolan/jq/blob/master/src/builtin.jq

复制其定义
相关问题