简体   繁体   中英

Delete all but one key-value pair from JSON

I have this:

{
  "service" : {
    "category" : "managed-object",
    "resource" : "attribute",
    "action" : "delete",
    "options" : {
      "uuid" : "#VALUE",
      "attributes" : {
        "name" : {
          "value" : "#VALUE"
        },
        "contactInfo" : "",
        "activationDate" : "",
        "deactivationDate" : "",
        "protectStopDate" : "",
        "processStartDate" : ""
      }
    }
  }
}

I need this:

{
  "service" : {
    "category" : "managed-object",
    "resource" : "attribute",
    "action" : "delete",
    "options" : {
      "uuid" : "#VALUE",
      "attributes" : {
        "name" : {
          "value" : "#VALUE"
        }
      }
    }
  }
}

Previously I had a similar problem that was a little bit more complicated, and I received this amazingly simple answer from someone here:

.service.options |= (del(.max, .objectGroupMember) | .attributes|={name})

That jq command works even here, but of course (.max, .objectGroupMember) does not make sense because it does not exist.

How can I achieve my desired result?

I found it in Weeble's answer to my earlier question:

'.service.options.attributes |= {name}'

A different take on the problem:

walk(if type=="object" then with_entries(select(.value!="")) else . end)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM