Issue
{
"status": "success",
"result": [{
"address_details": [{
"address": "135.138.168.78",
"serviceType": "go_gateway",
"addressType": "active",
"create_time": 1631879023,
"allow_listed": false
},
{
"address": "135.138.168.79",
"serviceType": "gp_gateway",
"addressType": "active",
"create_time": 1631879023,
"allow_listed": false
}
],
"zone": "Germany",
"addresses": [
"135.138.168.78",
"135.138.168.79"
],
"zone_subnet": [
"137.183.192.0/18",
"208.27.0.0/16",
"34.3.0.0/16",
"34.199.0.0/16",
"66.59.192.0/19",
"165.11.128.0/17",
"135.138.0.0/16",
"130.141.0.0/16",
"165.185.0.0/16"
],
"addresses_v6": [],
"address_details_v6": []
}]
}
Here is some sanitized fake example output of a command I'm using an API to pull.
I only need the actual IP addresses, not the subnets, and I only need the IP addresses once.
From the above example the hopeful output of this would just be the two IP addresses on two lines, like this
135.138.168.78
135.139.168.79
This is just one block of output from about 10 that are in the output/file, all formatted just like this.
Using my mediocre skillset and regex, I attempted to pull this information using grep and awk and had no real success, getting either multiples of the IP addresses in my output, or both the IPs and the subnets.
EDIT - updated with the validated JSON.
- now getting an error using the command jq --raw-output '.addresses[]'
"jq: error (at :37): Cannot iterate over null (null) exit status 5"
tried adding a "?" after the square brackets and it outputs nothing.
tried doing '.result.addresses[]' or '.status.addresses[]'
get "jq: error (at :37): Cannot index string with string "addresses" exit status 5"
Solution
so, again, totally new to this. Trial and error got me to this command.
jq --raw-output '.result | .[].addresses[]'
It works. I have no idea if this is a well-formatted or thought out jq command, but it outputs the IPs in both my test file and the real data.
Answered By - ksmm Answer Checked By - Willingham (WPSolving Volunteer)