[英]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.该 jq 命令甚至在这里也有效,但当然(.max, .objectGroupMember)
没有意义,因为它不存在。
How can I achieve my desired result?我怎样才能达到我想要的结果?
A different take on the problem:对问题的不同看法:
walk(if type=="object" then with_entries(select(.value!="")) else . end)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.