Reputation: 2973
So I have below sample codes
{
"host1": { "ip": "10.1.2.3" },
"host2": { "ip": "10.1.2.2" },
"host3": { "ip": "10.1.18.1" }
...
}
I need output of 'host1' and 'host3'
{
"host1": { "ip": "10.1.2.3" },
"host3": { "ip": "10.1.18.1" }
}
with command |jq .host1
, I can only get one
{
"ip": "10.1.2.3"
}
and I lost its key host1
as well
Upvotes: 1
Views: 40
Reputation: 43983
I need output of 'host1' and 'host3'
If you can use a hardcoded list of keys
, you can use the following JQ command to get only host1
and host3
:
{ host1, host3 }
{
"host1": {
"ip": "10.1.2.3"
},
"host3": {
"ip": "10.1.18.1"
}
}
This uses the Object Construction Shortcut Syntax as explained in there documentation and in this SO question: jq: Can I use the name of an argument in the code itself?.
Upvotes: 3