nyitguy
nyitguy

Reputation: 608

Simple jq command line query

I have a larger dataset, but here is a sample of my json file.

{"units": {
  "1000000043579e28": {
    "1_hostname": "unit-a",
    "3_lastCheckIn": "08/31/21 16:00",
    "screensProcesses": [
      "file1",
      "file2" ]},
  "10000000553ff4cd": {
    "1_hostname": "unit-b",
     "3_lastCheckIn": "08/31/21 16:00",
    "screensProcesses": [
      "file1" ]}
}}

I would like to use the jq binary in linux. I'm trying to do 2 queries.

  1. Units that have less than 2 "screenProcesses".
  2. Units that haven't checked in over 24 hours. (I can modify how I write the date in json file if needed.

Upvotes: 0

Views: 455

Answers (1)

jas
jas

Reputation: 10865

You might want to separate this into two questions since #1 is a relatively simple select while #2, dealing with time calculations is a topic in its own right. In any case, for #1 you can use with_entries to convert units to series of maps with key and value fields, operate on those with select and convert back again.

$ jq '{"units": .units | with_entries(select(.value."screensProcesses" | length < 2))}' file.json
{
  "units": {
    "10000000553ff4cd": {
      "1_hostname": "unit-b",
      "3_lastCheckIn": "08/31/21 16:00",
      "screensProcesses": [
        "file1"
      ]
    }
  }
}

Upvotes: 1

Related Questions