Select 使用 jq 基于散列中的键的对象
Select objects based on the key in hash using jq
我很乐意 select 基于散列中的键的对象,但我想要恢复原始对象格式。我怎样才能做到这一点?
原创
{
"google.com": {
"http": {
"dest_url": "http://whosebug.com"
},
"https": {
"dest_url": "http://github.com"
}
},
"aaa.com": {
"https": {
"dest_url": "https://github.com"
}
},
"bbb.com": {
"https": {
"dest_url": "https://microsoft.com"
}
},
"ccc.com": {
"https": {
"dest_url": "https://.com"
}
}
}
应该是
{
"google.com": {
"http": {
"dest_url": "http://whosebug.com"
},
"https": {
"dest_url": "http://github.com"
}
},
"bbb.com": {
"https": {
"dest_url": "https://microsoft.com"
}
}
}
我用 to_entries[] | select (.key == "google.com" or .key == "bbb.com") | [.key, .value]
试过了,但结果是这样的。我确定 [.key, .value]
部分是错误的,但我一直在思考如何解决这个问题。
[
"google.com",
{
"http": {
"dest_url": "http://whosebug.com"
},
"https": {
"dest_url": "http://github.com"
}
}
]
[
"bbb.com",
{
"https": {
"dest_url": "https://microsoft.com"
}
}
]
使用 with_entries(…)
,这是 to_entries | map(…) | from_entries
的快捷方式,后者将对象分解为 key/value 表示的数组 to_entries
,修改其中的每个项目数组 map
,并将其转换回原始结构 from_entries
.
jq 'with_entries(select(.key == "google.com" or .key == "bbb.com"))'
{
"google.com": {
"http": {
"dest_url": "http://whosebug.com"
},
"https": {
"dest_url": "http://github.com"
}
},
"bbb.com": {
"https": {
"dest_url": "https://microsoft.com"
}
}
}
我很乐意 select 基于散列中的键的对象,但我想要恢复原始对象格式。我怎样才能做到这一点?
原创
{
"google.com": {
"http": {
"dest_url": "http://whosebug.com"
},
"https": {
"dest_url": "http://github.com"
}
},
"aaa.com": {
"https": {
"dest_url": "https://github.com"
}
},
"bbb.com": {
"https": {
"dest_url": "https://microsoft.com"
}
},
"ccc.com": {
"https": {
"dest_url": "https://.com"
}
}
}
应该是
{
"google.com": {
"http": {
"dest_url": "http://whosebug.com"
},
"https": {
"dest_url": "http://github.com"
}
},
"bbb.com": {
"https": {
"dest_url": "https://microsoft.com"
}
}
}
我用 to_entries[] | select (.key == "google.com" or .key == "bbb.com") | [.key, .value]
试过了,但结果是这样的。我确定 [.key, .value]
部分是错误的,但我一直在思考如何解决这个问题。
[
"google.com",
{
"http": {
"dest_url": "http://whosebug.com"
},
"https": {
"dest_url": "http://github.com"
}
}
]
[
"bbb.com",
{
"https": {
"dest_url": "https://microsoft.com"
}
}
]
使用 with_entries(…)
,这是 to_entries | map(…) | from_entries
的快捷方式,后者将对象分解为 key/value 表示的数组 to_entries
,修改其中的每个项目数组 map
,并将其转换回原始结构 from_entries
.
jq 'with_entries(select(.key == "google.com" or .key == "bbb.com"))'
{
"google.com": {
"http": {
"dest_url": "http://whosebug.com"
},
"https": {
"dest_url": "http://github.com"
}
},
"bbb.com": {
"https": {
"dest_url": "https://microsoft.com"
}
}
}