Reputation: 90
I have a JSON file that looks like this:
$jsondata = '{
"ips": {
"10.20.30.40": [
{
"rhost": "DNS Name1.",
"rdata": [
"10.20.30.40"
],
"rrtype": "A (1)",
"ttl": 86400,
"geo": null,
"source": "DNSProvider1"
}
],
"40.50.60.70": [
{
"rhost": "DNS Name2.",
"rdata": [
"40.50.60.70"
],
"rrtype": "A (1)",
"ttl": 86400,
"geo": null,
"source": "DNSProvider1"
}
]
}
}'
I want to get all the TTLs (for example) of every IP address in the list.
I converted this JSON to Powershell PSCustomObject:
$obj = $jsondata | convertFrom-Json
and now I want to get all the TTLs, I tried to get the list of the IPs (as a start):
foreach ($ip in $a.ips) {write-host $ip }
and I'm not getting strings as a result, that's why I (probably) can't go inside and get the TTLs.
So my question: how can I get all the IPs as strings?
I believe that once I'll get an answer for that, I'll understand how I can go over all the IPs in the list.
Thanks!
Upvotes: 1
Views: 146
Reputation: 4348
foreach($ip in $obj.ips | Get-Member -MemberType NoteProperty)
{
Write-Host -Verbose ("IP Address {0} has TTL {1}" -f $ip.Name, $obj.ips."$($ip.Name)".ttl)
}
Get-Member
will get you the name of the property (which is the ip address) and not the value.
Upvotes: 2
Reputation: 90
Thanks Rubanov, that really helped!
And just to document the whole answer:
foreach($ip in $obj.ips | Get-Member -MemberType NoteProperty)
{
Write-Host -Verbose $obj.ips.$($ip.Name).ttl
}
Or:
($obj.ips | Get-Member -MemberType NoteProperty).Name | % {$obj.ips.$_.ttl}
Upvotes: 1