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

1.2k views Asked by At

2 Questions:

1) I want to keep the json as is but change the Timestamp to human readable date like "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

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.

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

There are 1 answers

2
peak On

1) Use |= rather than just |

2) One way to transform the fragment:

{
  "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):

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

I'll leave it to you to put all the pieces together :-)