none
Looping through $json variable and grabbing each id to add to an array RRS feed

  • Question

  • Given that I am processing the following $json variable, how would I grab each server id (i.e. 215d1109-216d-48c3-af8e-998bb9bc3ca0 and 440cf918-3ee0-4143-b289-f63e1d2000e6 in this case) and put it into an array?

    Right now $obj.server.id returns nothing (as expected), but $obj.server.id[0] returns an error message.

    clear
    [System.Reflection.Assembly]::LoadWithPartialName("System.Web.Extensions") | out-null
    $json = '
    {
        "servers": [
            {
                "admin_password": "qpYU66rKxmnK",
                "id": "215d1109-216d-48c3-af8e-998bb9bc3ca0",
                "links": [
                    {
                        "href": "http://openstack.example.com/v3/servers/<id>",
                        "rel": "self"
                    },
                    {
                        "href": "http://openstack.example.com/servers/<id>",
                        "rel": "bookmark"
                    }
                ]
            },
            {
                "admin_password": "wfksH3GTTseP",
                "id": "440cf918-3ee0-4143-b289-f63e1d2000e6",
                "links": [
                    {
                        "href": "http://openstack.example.com/v3/servers/<id>",
                        "rel": "self"
                    },
                    {
                        "href": "http://openstack.example.com/servers/<id>",
                        "rel": "bookmark"
                    }
                ]
            }
        ]
    }
    '
    $ser = New-Object System.Web.Script.Serialization.JavaScriptSerializer
    $obj = $ser.DeserializeObject($json)
    $obj.server.id;

    Please note that I am using Powershell 2.0.
    Saturday, July 11, 2015 2:12 PM

Answers