2

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 2

4

All you need is map_values and del.

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

demo in jqplay

Sign up to request clarification or add additional context in comments.

3 Comments

@Chloe: Click on tick-mark next to answer to mark the post resolved and upvote the ^ the answer if you found it useful
ok!! and if u don't mind, i have another question... I have other keys (other than "peers") such as "name", "id", and "memory". I want to delete the keys "memory", and at this moment I'm still using command del(.memory) so have to run two commands (along with the one you suggest). is there a way so i can run it in just one command?
@Chloe Stackoverflow does not encourage follow-up questions on the same post after accepting the answer. Please ask a new question with your proper input and an expected output
1

You can also use this syntax :

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.