JSON delete specific keys from nested objects without knowing key names

Viewed 186

I have a json file and want to delete some subkeys i don't need. Here is some part of the JSON file :

{
  "peers":{
    "swp1":{
      "hostname":"Spine-01",
      "remoteAs":65001,
      "version":4,
      "msgRcvd":452,
      "msgSent":459,
      "tableVersion":0,
      "outq":0,
      "inq":0,
      "peerUptime":"00:19:15",
      "peerUptimeMsec":1155000,
      "peerUptimeEstablishedEpoch":1635674862,
      "prefixReceivedCount":30,
      "pfxRcd":30,
      "pfxSnt":43,
      "state":"Established",
      "idType":"interface"
    },
    "swp2":{
      "hostname":"Spine-02",
      "remoteAs":65001,
      "version":4,
      "msgRcvd":452,
      "msgSent":459,
      "tableVersion":0,
      "outq":0,
      "inq":0,
      "peerUptime":"00:19:14",
      "peerUptimeMsec":1154000,
      "peerUptimeEstablishedEpoch":1635674863,
      "prefixReceivedCount":30,
      "pfxRcd":30,
      "pfxSnt":43,
      "state":"Established",
      "idType":"interface"
    }
  }
}

for example, i want to delete the "version" subkey, and i already tried this command

del(.peers.swp1.version, .peers.swp2.version)

and it worked well. But the thing is, the "swp1" and "swp2" are the interfaces and are something that can change their name, increase or decrease. So I need a command that works to delete the "version" subkeys no matter what the interface name is.

2 Answers

All you need is map_values and del.

.peers |= map_values(del(.version))

demo in jqplay

You can also use this syntax :

del(.peers[].version)|del(.memory)
Related