I am trying to grab 'n' item from a nested json array. The scenario is that I need to get the IP addresses from newly created cloud instances from my cloud provider so I can perform automation task with ansible. Here is a sample of the json output from the api that my cloud provider gives. (details obscured for privacy and security reasons)
[
{
"alerts": {
"cpu": 180,
"io": 10000,
"network_in": 10,
"network_out": 10,
"transfer_quota": 80
},
"backups": {
"enabled": false,
"last_successful": null,
"schedule": {
"day": null,
"window": null
}
},
"created": "2022-04",
"group": "",
"hypervisor": "kvm",
"id": 36084613,
"image": "ubuntu20.04",
"ipv4": [
"12.34.56.78", #<--- Need to grab this public address
"192.168.x.x" #<--- and this private address
],
"ipv6": "0000::0000/128",
"label": "node-1",
"region": "us",
"specs": {
"disk": 81920,
"memory": 4096,
"transfer": 4000,
"vcpus": 2
},
"status": "running",
"tags": [],
"type": "standard",
"updated": "2022-04",
"watchdog_enabled": true
}
]
I need to get the public IP address to I can add the node to an inventory file. So far, I have managed to get the following:
$ cat json.json | jq -r '.[0].ipv4'
[
"12.34.56.78",
"192.168.x.x"
]
I can get what I want by repiping into jq, but I feel there has to be a more elegant way to do so.
$ cat json.json | jq -r '.[0].ipv4' | jq -r '.[0]'
12.34.56.78
$ cat json.json | jq -r '.[0].ipv4' | jq -r '.[1]'
192.168.x.x
New to posting on StackOverflow so I apologize in advance if someone already answered this on another thread. I looked around and couldn't find what I was looking for. Thanks!