methuselah
methuselah

Reputation: 13216

Accessing values in an object resulting of a parsed JSON

Have a look to the following JSON (too big to fit here), I'am not able to get value using

[System.Reflection.Assembly]::LoadWithPartialName("System.Web.Extensions")
$ser = New-Object System.Web.Script.Serialization.JavaScriptSerializer
$jsonObj = $ser.DeserializeObject($json)

$jsonObj.servers | Where-Object {$_.name -EQ 'Tendaji'} | `
  select-object { $_.ipv4addr }

I receive

   $_.ipv4Address
    --------------
    113.55.212.113

How would I get it to only just return 113.55.212.113?

Upvotes: 0

Views: 25

Answers (1)

JPBlanc
JPBlanc

Reputation: 72680

Have a look to :

$_.ipv4Address | get-member
Get-Member -InputObject $_.ipv4Address

You may have an array

$_.ipv4Address[0]

Be carreful you've got an error inside your JSON :

"name": "Aboubacar""ipv4addr": "143.179.56.126"

should be :

"name": "Aboubacar",
"ipv4addr": "143.179.56.126"

Then

$jsonObj.servers[0].ipv4addr

gives

113.55.212.113

So in your code your can use :

($jsonObj.servers | Where-Object {$_.name -EQ 'Tendaji'}).ipv4addr

Upvotes: 1

Related Questions