简体   繁体   English

jq json解析-使用todate将时间戳替换为date,并整理一个数组

[英]jq json parsing - replace timestamp to date using todate, and flat an array

2 Questions: 2个问题:

1) I want to keep the json as is but change the Timestamp to human readable date like "2016-12-19T09:21:35Z" 1)我想保持json不变,但将时间戳更改为人类可读的日期,例如“ 2016-12-19T09:21:35Z”

{
  "Action": "ALLOW",
  "Timestamp": 1482139256.274,
  "Request": {
    "Country": "US",
    "URI": "/version/moot/beta.json",
    "Headers": [
      {
        "Name": "Host",
        "Value": "static.tiza.com"
      },
      {
        "Name": "User-Agent",
        "Value": "Faraday v0.9.2"
      },
      {
        "Name": "Accept-Encoding",
        "Value": "gzip;q=1.0,deflate;q=0.6,identity;q=0.3"
      },
      {
        "Name": "Accept",
        "Value": "*/*"
      },
      {
        "Name": "X-Newrelic-Id",
        "Value": "Vgcs5gbFU123dFBWGwIdAVFdrXBwc="
      },
      {
        "Name": "X-Newrelic-Transaction",
        "Value": "PxQDQVlzZVUd3NKQcrEwWwU"
      }
    ],
    "ClientIP": "107.22.17.51",
    "Method": "GET",
    "HTTPVersion": "HTTP/1.1"
  },
  "Weight": 1
}

I know I can do it using 'todate' jq feature but I lose all other data 我知道我可以使用“ todate” jq功能来做到这一点,但是我丢失了所有其他数据

sh# cat temp.json | jq -r '.SampledRequests[].Timestamp | todate'
2016-12-19T09:21:44Z

--------- Updated -------- - - - - - 更新 - - - -

second question: 2)How do I take the content of .Headers[] out of the array under the "Request{}" level. 第二个问题:2)我如何在“ Request {}”级别下从数组中取出.Headers []的内容。

from: 从:

{
  "TimeWindow": {
    "EndTime": 1482156660,
    "StartTime": 1482156420
  },
  "SampledRequests": [
    {
      "Action": "ALLOW",
      "Timestamp": 1482139256.274,
      "Request": {
        "Country": "US",
        "URI": "/version/moot/beta.json",
        "Headers": [
          {
            "Name": "Host",
            "Value": "static.tiza.com"
          },
          {
            "Name": "X-Newrelic-Transaction",
            "Value": "PxQDQVlzZVUd3NKQcrEwWwU"
          }
        ],
        "ClientIP": "107.22.17.51",
        "Method": "GET",
        "HTTPVersion": "HTTP/1.1"
      },
      "Weight": 1
    }
  ],
  "PopulationSize": 89
}

To: 至:

{
    "TimeWindow.EndTime": 1482156660,
    "TimeWindow.StartTime": 1482156420,
    "Action": "ALLOW",
    "Timestamp": 1482139256.274,
    "Request.Country": "US",
    "Request.URI": "/version/moot/beta.json",
    "Headers.Host": "static.tiza.com",
    "Headers.X-Newrelic-Transaction": "PxQDQVlzZVUd3NKQcrEwWwU",
    "ClientIP": "107.22.17.51",
    "Method": "GET",
    "HTTPVersion": "HTTP/1.1",
    "Weight": 1,
    "PopulationSize": 89
}

Thanks a lot, 非常感谢,

Moshe 摩西

1) Use |= rather than just | 1)使用| =而不是|

2) One way to transform the fragment: 2)一种转换片段的方法:

{
  "Headers": [
    {
      "Name": "Host",
      "Value": "static.tiza.com"
    },
    {
      "Name": "User-Agent",
      "Value": "Faraday v0.9.2"
    }
  ]
}

as required would be using the filter: 根据需要将使用过滤器:

.Headers[] | { ("Headers." + .Name): .Value }

In your case, you could therefore use the following filter for (2): 因此,您可以针对(2)使用以下过滤器:

.SampledRequests[].Request.Headers[] |=
    { ("Headers." + .Name): .Value }

I'll leave it to you to put all the pieces together :-) 我将它交给您,将所有片段放在一起:-)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM