skip to Main Content

Hello I’ve the following json and try to get all parents who have the "linux-image-amd64"

{
    "web-prode-01.example.de": "ERROR: Problem encountered installing package(s). Additional info follows:nnchanges:n    ----------n    linux-image-amd64:n        ----------n        new:n        old:n            6.1.55-1nerrors:n    - Running scope as unit: run-rad31bddd35ec452b9d67fef004ce3daf.scopen      E: Sub-process /usr/bin/dpkg returned an error code (1)",
    "web-prode-04.example.de": {
        "linux-image-amd64": {
            "old": "5.10.197-1",
            "new": "5.10.205-2"
        },
        "linux-image-5.10.0-27-amd64": {
            "old": "",
            "new": "5.10.205-2"
        }
    },
    "lmrelaunch.example.de": {
        "linux-image-6.1.0-0.deb11.13-amd64": {
            "old": "",
            "new": "6.1.55-1~bpo11+1"
        },
        "linux-image-amd64": {
            "old": "6.1.38-4~bpo11+1",
            "new": "6.1.55-1~bpo11+1"
        }
    }   
}

I tried jq -r 'keys[] as $k | "($k) (.[$k]."linux-image-amd64")"' but I just got one entry instead of two

2

Answers


  1. to_entries[] | select(.value | type == "object" and has("linux-image-amd64")) | .key
    

    working demo here

    Returns :

    "web-prode-04.example.de"
    "lmrelaunch.example.de"
    
    Login or Signup to reply.
  2. You should check if the context has that key. Also, to prevent the string from failing when trying to apply has on it, either filter for objects, or suppress the error with ?. In either case, use this condition to filter the results using select.

    keys[] as $k | .[$k] | objects | select(has("linux-image-amd64"))
    # or
    keys[] as $k | .[$k] | select(has("linux-image-amd64")?)
    
    {
      "linux-image-6.1.0-0.deb11.13-amd64": {
        "old": "",
        "new": "6.1.55-1~bpo11+1"
      },
      "linux-image-amd64": {
        "old": "6.1.38-4~bpo11+1",
        "new": "6.1.55-1~bpo11+1"
      }
    }
    {
      "linux-image-amd64": {
        "old": "5.10.197-1",
        "new": "5.10.205-2"
      },
      "linux-image-5.10.0-27-amd64": {
        "old": "",
        "new": "5.10.205-2"
      }
    }
    

    You can then construct your output using $k, e.g.:

    keys[] as $k | .[$k] | select(has("linux-image-amd64")?) | $k
    
    lmrelaunch.example.de
    web-prode-04.example.de
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search