I would like to have output like this with second IP:
intranet.site.com : 59.89.8.8
running this command
cat ./droplet_list.json | jq '.[] | .name , .networks.v4[].ip_address'
I have got
"intranet.site.com"
"10.136.95.40"
"59.89.8.8"
"57.23.3.1"
I was trying run:
cat ./droplet_list.json | jq '.[] | .name , .networks.v4[].ip_address[1]'
and got the error
"intranet.site.com" jq: error (at <stdin>:36035): Cannot index string with number
My JSON file
[
{
"id": 106,
"name": "intranet.site.com",
"networks": {
"v4": [
{
"ip_address": "10.136.95.40",
"netmask": "255.255.0.0",
"gateway": "10.136.0.1"
},
{
"ip_address": "59.89.8.8",
"netmask": "255.255.240.0",
"gateway": "159.0.0.1"
},
{
"ip_address": "57.23.3.1",
"netmask": "255.255.252.0",
"gateway": "157.0.0.1"
}
]
}
}
]
Thanks
CodePudding user response:
Just use
to concatenate the strings:
jq -r '.[] | .name ": " .networks.v4[].ip_address'
intranet.site.com: 10.136.95.40
intranet.site.com: 59.89.8.8
intranet.site.com: 57.23.3.1
You may also give the .v4
array specific indices (instead of iterating over all of its items with []
):
jq - '.[] | .name ": " .networks.v4[1].ip_address'
intranet.site.com: 59.89.8.8
CodePudding user response:
One option would be using map
along with
operator such as
jq -r 'map(.name ": " .networks.v4[].ip_address)[]' yourfile.json