如果为true,则在JSON文档上使用-match返回不同的结果

时间:2015-07-16 20:05:15

标签: regex json powershell-v2.0

如果servers|addresses|addr返回true,相对于找到指定$json -Match值的范围,返回-Match中第一个找到的值的最佳方法是什么?

例如,使用下面的$ json变量:

If $json -Match "Aboubacar" returns true, then $result = "143.179.56.126"
If $json -Match "Tendaji" returns true, then $result = "113.55.212.113"

我想在Powershell版本2中完成此任务。

$json='
{
    "servers": [
        {
            "addresses": {
                "": [
                    {
                        "addr": "113.55.212.113",
                        "OS-EXT-IPS-MAC:mac_addr": "aa:bb:cc:dd:ee:ff",
                        "OS-EXT-IPS:type": "fixed",
                        "version": 4
                    }
                ]
            },
            "created": "2013-08-15T12:04:04Z",
            "flavor": {
                "id": "1",
                "links": [
                    {
                        "href": "http://openstack.example.com/flavors/1",
                        "rel": "bookmark"
                    }
                ]
            },
            "hostId": "117535ce0eda7ee02ebffe2c976173629385481ae3f2bded5e14a66b",
            "id": "ae114799-9164-48f5-a036-6ef9310acbc4",
            "image": {
                "id": "70a599e0-31e7-49b7-b260-868f441e862b",
                "links": [
                    {
                        "href": "http://openstack.example.com/images/70a599e0-31e7-49b7-b260-868f441e862b",
                        "rel": "bookmark"
                    }
                ]
            },
            "key_name": null,
            "links": [
                {
                    "href": "http://openstack.example.com/v2.1/servers/ae114799-9164-48f5-a036-6ef9310acbc4",
                    "rel": "self"
                },
                {
                    "href": "http://openstack.example.com/servers/ae114799-9164-48f5-a036-6ef9310acbc4",
                    "rel": "bookmark"
                }
            ],
            "metadata": {
                "My Server Name": "Tendaji"
            },
            "name": "Tendaji"
        },
        {
            "addresses": {
                "": [
                    {
                        "addr": "143.179.56.126",
                        "OS-EXT-IPS-MAC:mac_addr": "aa:bb:cc:dd:ee:ff",
                        "OS-EXT-IPS:type": "fixed",
                        "version": 4
                    }
                ]
            },
            "created": "2013-08-15T12:04:04Z",
            "flavor": {
                "id": "1",
                "links": [
                    {
                        "href": "http://openstack.example.com/flavors/1",
                        "rel": "bookmark"
                    }
                ]
            },
            "hostId": "117535ce0eda7ee02ebffe2c976173629385481ae3f2bded5e14a66b",
            "id": "ae114799-9164-48f5-a036-6ef9310acbc4",
            "image": {
                "id": "70a599e0-31e7-49b7-b260-868f441e862b",
                "links": [
                    {
                        "href": "http://openstack.example.com/images/70a599e0-31e7-49b7-b260-868f441e862b",
                        "rel": "bookmark"
                    }
                ]
            },
            "key_name": null,
            "links": [
                {
                    "href": "http://openstack.example.com/v2.1/servers/ae114799-9164-48f5-a036-6ef9310acbc4",
                    "rel": "self"
                },
                {
                    "href": "http://openstack.example.com/servers/ae114799-9164-48f5-a036-6ef9310acbc4",
                    "rel": "bookmark"
                }
            ],
            "metadata": {
                "My Server Name": "Aboubacar"
            },
            "name": "Aboubacar"
        }
    ]
}'

$json -Match "Aboubacar"  # if true return true, then $result = "143.179.56.126" as seen in `servers|addresses|addr`
$json -Match "Tendaji" # true, then $result = "113.55.212.113" as seen in `servers|addresses|addr`

1 个答案:

答案 0 :(得分:1)

最简单的方法是将JSON字符串转换为对象,然后在PowerShell中使用简单的对象选择功能。但是,此方法需要在“地址”下使用非空白属性名称。

像这样:

$json='
{
    "servers": [
        {
            "addresses": {
                "addr": [
                    {
                        "addr": "113.55.212.113",
                        "OS-EXT-IPS-MAC:mac_addr": "aa:bb:cc:dd:ee:ff",
                        "OS-EXT-IPS:type": "fixed",
                        "version": 4
                    }
                ]
            },
            "created": "2013-08-15T12:04:04Z",
            "flavor": {
                "id": "1",
                "links": [
                    {
                        "href": "http://openstack.example.com/flavors/1",
                        "rel": "bookmark"
                    }
                ]
            },
            "hostId": "117535ce0eda7ee02ebffe2c976173629385481ae3f2bded5e14a66b",
            "id": "ae114799-9164-48f5-a036-6ef9310acbc4",
            "image": {
                "id": "70a599e0-31e7-49b7-b260-868f441e862b",
                "links": [
                    {
                        "href": "http://openstack.example.com/images/70a599e0-31e7-49b7-b260-868f441e862b",
                        "rel": "bookmark"
                    }
                ]
            },
            "key_name": null,
            "links": [
                {
                    "href": "http://openstack.example.com/v2.1/servers/ae114799-9164-48f5-a036-6ef9310acbc4",
                    "rel": "self"
                },
                {
                    "href": "http://openstack.example.com/servers/ae114799-9164-48f5-a036-6ef9310acbc4",
                    "rel": "bookmark"
                }
            ],
            "metadata": {
                "My Server Name": "Tendaji"
            },
            "name": "Tendaji"
        },
        {
            "addresses": {
                "addr": [
                    {
                        "addr": "143.179.56.126",
                        "OS-EXT-IPS-MAC:mac_addr": "aa:bb:cc:dd:ee:ff",
                        "OS-EXT-IPS:type": "fixed",
                        "version": 4
                    }
                ]
            },
            "created": "2013-08-15T12:04:04Z",
            "flavor": {
                "id": "1",
                "links": [
                    {
                        "href": "http://openstack.example.com/flavors/1",
                        "rel": "bookmark"
                    }
                ]
            },
            "hostId": "117535ce0eda7ee02ebffe2c976173629385481ae3f2bded5e14a66b",
            "id": "ae114799-9164-48f5-a036-6ef9310acbc4",
            "image": {
                "id": "70a599e0-31e7-49b7-b260-868f441e862b",
                "links": [
                    {
                        "href": "http://openstack.example.com/images/70a599e0-31e7-49b7-b260-868f441e862b",
                        "rel": "bookmark"
                    }
                ]
            },
            "key_name": null,
            "links": [
                {
                    "href": "http://openstack.example.com/v2.1/servers/ae114799-9164-48f5-a036-6ef9310acbc4",
                    "rel": "self"
                },
                {
                    "href": "http://openstack.example.com/servers/ae114799-9164-48f5-a036-6ef9310acbc4",
                    "rel": "bookmark"
                }
            ],
            "metadata": {
                "My Server Name": "Aboubacar"
            },
            "name": "Aboubacar"
        }
    ]
}'

[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 { $_.addresses.addr[0].addr }

$jsonObj.servers | Where-Object {$_.name -EQ 'Aboubacar'} | `
  select-object { $_.addresses.addr[0].addr }