I have an input:
$ ip -json route show > ip_routes.json
$ cat ip_routes.json # I've pretty printed this for easier reading.
[
{
"dst": "default",
"gateway": "10.1.201.1",
"dev": "eth0"
},
{
"dst": "10.1.201.0/24",
"dev": "eth0",
"prefsrc": "10.1.201.74"
},
{
"dst": "10.1.202.0/24",
"dev": "eth0",
"prefsrc": "10.1.202.74"
},
{
"dst": "10.244.0.0/24",
"dev": "cni0",
"prefsrc": "10.244.0.1"
},
{
"dst": "10.244.0.0/22",
"dev": "cni0",
"scope": "link",
"flags": []
}
]
I can do a query where dev is eth0 and not the default gateway:
$ jq '. [] | if .dev == "eth0" and .dst != "default" then . else empty end' ip_routes.json
{
"dst": "10.1.201.0/24",
"dev": "eth0",
"prefsrc": "10.1.201.74"
}
{
"dst": "10.1.202.0/24",
"dev": "eth0",
"prefsrc": "10.1.202.74"
}
If I try to select specific properties in the output their association with the object is lost:
jq '. [] | if .dev == "eth0" and .dst != "default" then .dev, .prefsrc else empty end' ip_routes.json
"eth0"
"10.1.201.74"
"eth0"
"10.1.202.74"
How do get something like this?
[
{
"eth0",
"10.1.201.74"
}
,
{
"eth0",
"10.1.202.74"
}
]
https://jqplay.org/s/GsAwBlKJJ92DNAO