Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using jq to extract specific property values and output on a single line

Tags:

json

jq

I've this ElasticSearch snapshot output and would like to reduce it to print, on a single line, from each snapshot the values of the end_time_in_millis and snapshot property separate by a space:

1429609790767 snapshot_1
1429681169896 snapshot_2

Basically the output of

  • cat data | jq '.snapshots[].end_time_in_millis' and
  • cat data | jq '.snapshots[].snapshot'

but combined on one line.

I was looking at map but couldn't make out how to apply it; also reading this answer I tried:

cat data  | jq '.snapshots[] | map(. |= with_entries( select( .key == ( "snapshot") ) ) )'

But that produces lots of errors and null output.

The data:

{
  "snapshots": [
    {
      "shards": {
        "successful": 1,
        "failed": 0,
        "total": 1
      },
      "failures": [],
      "snapshot": "snapshot_1",
      "indices": [
        "myindex1"
      ],
      "state": "SUCCESS",
      "start_time": "2015-04-21T09:45:47.041Z",
      "start_time_in_millis": 1429609547041,
      "end_time": "2015-04-21T09:49:50.767Z",
      "end_time_in_millis": 1429609790767,
      "duration_in_millis": 243726
    },
    {
      "shards": {
        "successful": 1,
        "failed": 0,
        "total": 1
      },
      "failures": [],
      "snapshot": "snapshot_2",
      "indices": [
        "myindex1"
      ],
      "state": "SUCCESS",
      "start_time": "2015-04-22T05:36:02.333Z",
      "start_time_in_millis": 1429680962333,
      "end_time": "2015-04-22T05:39:29.896Z",
      "end_time_in_millis": 1429681169896,
      "duration_in_millis": 207563
    }
  ]
}
like image 334
mark Avatar asked May 01 '15 07:05

mark


People also ask

How extract JSON data from jq?

The simplest way to extract data from a JSON file is to provide a key name to obtain its data value. Type a period and the key name without a space between them. This creates a filter from the key name. We also need to tell jq which JSON file to use.

Does jq use JSONPath?

jp is a JSON processor for the command line using JSONPath (aka "a simpler jq, and with JSONPath").

What is jq output?

jq usually outputs non-ASCII Unicode codepoints as UTF-8, even if the input specified them as escape sequences (like "\u03bc"). Using this option, you can force jq to produce pure ASCII output with every non-ASCII character replaced with the equivalent escape sequence. --unbuffered.

What is jq slurp?

“Slurp” tells jq to read every line of the input JSON lines and treat the entire group as one huge array of objects.


1 Answers

Use this filter:

.snapshots[] | "\(.end_time_in_millis) \(.snapshot)"

This builds up a string for each of the snapshots consisting of the end time and the snapshot name.

Just make sure you use the -r option to get the raw output.

like image 154
Jeff Mercado Avatar answered Oct 27 '22 02:10

Jeff Mercado