jq:删除具有空字符串值的键 [英] jq: Remove keys with empty string values
本文介绍了jq:删除具有空字符串值的键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有以下JSON:
{
"data": [
{
"{#NAME}": "Test 1",
"{#ID}": "1",
"{#IP}": "192.168.1.2:80"
},
{
"{#NAME}": "Test 2",
"{#ID}": "2",
"{#IP}": ""
},
{
"{#NAME}": "Test 3",
"{#ID}": "3",
"{#IP}": "192.168.1.3:80"
},
{
"{#NAME}": "Test 4",
"{#ID}": "4",
"{#IP}": "192.168.1.4:80"
},
{
"{#NAME}": "Test 5",
"{#ID}": "5",
"{#IP}": ""
}
]
}
但是我想返回:
{
"data": [
{
"{#NAME}": "Test 1",
"{#ID}": "1",
"{#IP}": "192.168.1.2"
},
{
"{#NAME}": "Test 2",
"{#ID}": "2",
},
{
"{#NAME}": "Test 3",
"{#ID}": "3",
"{#IP}": "192.168.1.3"
},
{
"{#NAME}": "Test 4",
"{#ID}": "4",
"{#IP}": "192.168.1.4"
},
{
"{#NAME}": "Test 5",
"{#ID}": "5",
}
]
}
我对使用JQ非常陌生,不确定如何去做.我通读了 GitHub上的问题页面,但没有一个示例适合我
I'm very new to using JQ and not sure how I can go about doing this. I read through this issue page on GitHub but none of the example seem to work for me.
我还需要删除端口号和冒号.有可能吗?
I also need to remove the port number and colon. Is that possible?
推荐答案
您可以在下面执行操作,使用
You can do below, to select values that are not empty using with_entries(expr)
, with the expr being the condition that excludes the empty fields.
还再次使用.value
字段删除与包含端口字符串的正则表达式匹配的字符串.
Also use the .value
field again to remove the string that matches the regex containing the port string.
jq '.data |= map(with_entries(select(.value != "") | .value |= sub(":[0-9][0-9]$"; "")))'
这篇关于jq:删除具有空字符串值的键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文