[英]Delete all but one key-value pair from JSON
我有這個:
{
"service" : {
"category" : "managed-object",
"resource" : "attribute",
"action" : "delete",
"options" : {
"uuid" : "#VALUE",
"attributes" : {
"name" : {
"value" : "#VALUE"
},
"contactInfo" : "",
"activationDate" : "",
"deactivationDate" : "",
"protectStopDate" : "",
"processStartDate" : ""
}
}
}
}
我需要這個:
{
"service" : {
"category" : "managed-object",
"resource" : "attribute",
"action" : "delete",
"options" : {
"uuid" : "#VALUE",
"attributes" : {
"name" : {
"value" : "#VALUE"
}
}
}
}
}
以前我有一個類似的問題有點復雜,我從這里的某人那里得到了這個非常簡單的答案:
.service.options |= (del(.max, .objectGroupMember) | .attributes|={name})
該 jq 命令甚至在這里也有效,但當然(.max, .objectGroupMember)
沒有意義,因為它不存在。
我怎樣才能達到我想要的結果?
我在 Weeble 對我之前的問題的回答中找到了它:
'.service.options.attributes |= {name}'
對問題的不同看法:
walk(if type=="object" then with_entries(select(.value!="")) else . end)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.