鉴于此JSON片段,我试图找出与特定" instance-id"相对应的密钥名称。使用jq。
我正在尝试这个:
jq '.machines | keys as $x | if .[]."instance-id"=="wdc7ae" then <some-code-to-get-the-key> else empty end'
其中$ x包含所有机器号。和&#34; wdc7ae&#34;是具体的&#34; instance-id&#34;值
{
"model": {
"name": "maas2",
"controller": "ctr-xenial",
"cloud": "maas",
"version": "2.0.1"
},
"machines": {
"1": {
"dns-name": "10.4.0.181",
"instance-id": "wdc7ae",
"series": "xenial"
},
"2": {
"dns-name": "10.4.0.182",
"instance-id": "7mx74d",
"series": "xenial"
},
"3": {
"dns-name": "10.4.0.183",
"instance-id": "rgn3ca",
"series": "xenial"
},
"4": {
"dns-name": "10.4.0.184",
"instance-id": "kmy4bh",
"series": "xenial"
},
"5": {
"dns-name": "10.4.0.185",
"instance-id": "eb38c7",
"series": "xenial"
}
}
}
&#13;
非常感谢任何帮助。
答案 0 :(得分:2)
谢谢&#39;金鱼&#39;来自#jq irc频道。这是&#39;金鱼&#39;的命令。分享和工作就像一个魅力对我来说:
.machines | to_entries[] | select(.value."instance-id" == "wdc7ae").key