1

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!

2
  • Perhaps that's a daft remark, but you're not using the same variable between both your examples: $objand $a. Is that a typo? Can you show what results you're getting? Commented Aug 26, 2016 at 14:49
  • $obj.ips not $a.ips Commented Aug 26, 2016 at 14:49

2 Answers 2

2
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.

Sign up to request clarification or add additional context in comments.

Comments

1

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} 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.