Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JQ: Filtering for keys

I'm trying to use the JQ command to filter a json object to selectively extract keys. Here's a sample object that I've placed in file x.txt:

{
  "activities" : {

    "-KSndgjqvmQkWVKHCpLh" : {
      "create_device" : "...",
      "stop_time_utc" : "2016-11-01T23:08:08Z"
    },

    "-KSoBSrBh6PZcjRocGD7" : {
      "create_device" : "..."
    },

    "-KSptboGjo8g4bieUbGM" : {
      "create_device" : "...",
      "stop_time_utc" : "2017-01-17T23:08:08Z"
    }

  }
}

The following command can extract all of the activity keys:

cat x.txt | jq '.activities | keys'
[
  "-KSndgjqvmQkWVKHCpLh",
  "-KSoBSrBh6PZcjRocGD7",
  "-KSptboGjo8g4bieUbGM"
]

I've been googling and experimenting for a few hours trying to filter the object to select only the activity entries that have a stop_time_utc value, and use something like a "select(.stop_time_utc | fromdateiso8601 > now)" to only pick activities that have expired. For example, I'd like to use filters to create an array from the sample object with only the one relevant entry:

[
  "-KSndgjqvmQkWVKHCpLh"
]

Is attempting this with the keys option the wrong route? Any ideas or suggestions would be much appreciated.

like image 743
DThimsen Avatar asked Dec 02 '16 01:12

DThimsen


1 Answers

with_entries/1 is your friend, e.g.:

.activities | with_entries( select(.value | has("stop_time_utc") ) )

produces:

{
  "-KSndgjqvmQkWVKHCpLh": {
    "create_device": "...",
    "stop_time_utc": "2016-11-01T23:08:08Z"
  },
  "-KSptboGjo8g4bieUbGM": {
    "create_device": "...",
    "stop_time_utc": "2017-01-17T23:08:08Z"
  }

It's now easy to add additional selection criteria, extract the key names of interest, etc. For example:

.activities
| with_entries( select( (.value.stop_time_utc? | fromdateiso8601?) < now ) )
| keys
like image 189
peak Avatar answered Sep 19 '22 17:09

peak