【问题标题】:Change key value and print both the changed value and the original value更改键值并打印更改后的值和原始值
【发布时间】:2020-04-23 06:27:57
【问题描述】:

刚刚发现 jq,我想更改键的值并打印更改后的键值和原始键值。

原始样本文件

{
  "level": "info",
  "ts": 1587565958.8860734,
  "logger": "http.log.access.log0",
  "msg": "handled request",
  "request": {
    "method": "GET",
    "uri": "/info.php",
    "proto": "HTTP/1.1",
    "remote_addr": "192.168.0.92:42170"
    }
}

过滤器

.ts |= strftime("%Y-%m-%d %H:%M:%S") | .ts, .request[]

输出

"2020-04-22 14:32:38"
"GET"
"/info.php"
"HTTP/1.1"
"192.168.0.92:42170"

需要的输出

1587565958.8860734
"2020-04-22 14:32:38"
"GET"
"/info.php"
"HTTP/1.1"
"192.168.0.92:42170"

jq 玩

jq play

【问题讨论】:

    标签: json jq


    【解决方案1】:

    问,你会收到:

    • 调整您的查询 -
    .ts,
    (.ts |= strftime("%Y-%m-%d %H:%M:%S") | .ts, .request[])
    
    • 或更高效 -
    (.ts | ., strftime("%Y-%m-%d %H:%M:%S")),
    .request[]
    

    【讨论】:

    • 太快了!谢谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-11-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-26
    • 2014-03-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多