如何在powershell中迭代json,而无法指定值。

时间:2022-09-23 07:42:42

I need to read some information from json in Powershell. The problem that I am having is that after the "paths" section I cannot specify the next name e.g. what the path is called because it changes. Then after that I need to get the "get" section and then the "parameters" section and finally get the name and the required fields there.

我需要从Powershell的json中读取一些信息。我遇到的问题是,在“路径”部分后,我无法指定下一个名称,例如调用路径是因为它发生了变化。然后我需要获取“获取”部分,然后是“参数”部分,最后获取名称和必填字段。

I can get the hostname by using $info.host and the paths like $info.paths. Then I get the individual path names by looping through the path's to get each name like this.

我可以使用$ info.host和$ info.paths之类的路径获取主机名。然后我通过循环遍历路径来获取各个路径名称,以获得这样的每个名称。

foreach($item in $pName)
{
    $item  
}

But then that is as far as I can get I tried using $item.childItem but it doesn't work. Is there anyway to use something like $item.paths.childItems[$1].get.parameters and then get the name and the required field values?

但那就是我可以得到我尝试使用$ item.childItem,但它不起作用。无论如何使用$ item.paths.childItems [$ 1] .get.parameters之类的东西然后获取名称和必填字段值?

The json looks like this:

json看起来像这样:

{
"swagger": "2.0",
"info": {
"version": "v1",
"title": "Requirements.API"
},
"host": "replica.net",
"schemes": [
"http"
],
"paths": {
"/": {
"get": {
"tags": [
"Alive"
],
"operationId": "Alive_Get",
"consumes": [],
"produces": [
"application/json",
"text/json",
"application/xml",
"text/xml"
],
"responses": {
"200": {
"description": "OK",
"schema": {
"$ref": "#/definitions/Object"
}
}
},
"deprecated": false
}
},
"/requirements/email-docs": {
"get": {
"tags": [
"CustomerRequirement"
],
"operationId": "",
"consumes": [],
"produces": [
"application/json",
"text/json",
"application/xml",
"text/xml"
],
"parameters": [
{
"name": "name",
"in": "query",
"required": true,
"type": "integer",
"format": "int32"
}
],
"responses": {
"200": {
"description": "OK",
"schema": {
"type": "array",
"items": {
"$ref": "#/definitions/UploadResponse"
}
}
}
},
"deprecated": false
}
},

2 个解决方案

#1


0  

I got it using this:

我用它来得到它:

  for($i=0;$i-lt$pName.Count;$i++)
            {
                $test | Select -ExpandProperty paths | select -ExpandProperty $pName[$i] | select -ExpandProperty get

            }

#2


0  

Your JSON file is not clearly formatted and misses some closing parentheses. Nevertheless, after closing the JSON file and read it into PowerShell (ConvertFrom-Json), your PowerShell object looks like this (using PSON or Write-Log):

您的JSON文件格式不清晰,并且错过了一些右括号。然而,在关闭JSON文件并将其读入PowerShell(ConvertFrom-Json)后,您的PowerShell对象看起来像这样(使用PSON或Write-Log):

{
        swagger: "2.0",
        info: {
                version: "v1",
                title: "Requirements.API"
        },
        host: "replica.net",
        schemes: @(
                "http"
        ),
        paths: {
                /: {
                        get: {
                                tags: @(
                                        "Alive"
                                ),
                                operationId: "Alive_Get",
                                consumes: @(),
                                produces: @(
                                        "application/json",
                                        "text/json",
                                        "application/xml",
                                        "text/xml"
                                ),
                                responses: {
                                        200: {
                                                description: "OK",
                                                schema: {
                                                        $ref: "#/definitions/Object"
                                                }
                                        }
                                },
                                deprecated: $False
                        }
                },
                /requirements/email-docs: {
                        get: {
                                tags: @(
                                        "CustomerRequirement"
                                ),
                                operationId: "",
                                consumes: @(),
                                produces: @(
                                        "application/json",
                                        "text/json",
                                        "application/xml",
                                        "text/xml"
                                ),
                                parameters: @(
                                        {
                                                name: "name",
                                                in: "query",
                                                required: $True,
                                                type: "integer",
                                                format: "int32"
                                        }
                                ),
                                responses: {
                                        200: {
                                                description: "OK",
                                                schema: {
                                                        type: "array",
                                                        items: {
                                                                $ref: "#/definitions/UploadResponse"
                                                        }
                                                }
                                        }
                                },
                                deprecated: $False
                        }
                }
        }
}

So I guess the properties/values where you looking for are:

所以我想你要找的属性/值是:

$Item.paths.PSObject.Properties | Select -ExpandProperty Name

#1


0  

I got it using this:

我用它来得到它:

  for($i=0;$i-lt$pName.Count;$i++)
            {
                $test | Select -ExpandProperty paths | select -ExpandProperty $pName[$i] | select -ExpandProperty get

            }

#2


0  

Your JSON file is not clearly formatted and misses some closing parentheses. Nevertheless, after closing the JSON file and read it into PowerShell (ConvertFrom-Json), your PowerShell object looks like this (using PSON or Write-Log):

您的JSON文件格式不清晰,并且错过了一些右括号。然而,在关闭JSON文件并将其读入PowerShell(ConvertFrom-Json)后,您的PowerShell对象看起来像这样(使用PSON或Write-Log):

{
        swagger: "2.0",
        info: {
                version: "v1",
                title: "Requirements.API"
        },
        host: "replica.net",
        schemes: @(
                "http"
        ),
        paths: {
                /: {
                        get: {
                                tags: @(
                                        "Alive"
                                ),
                                operationId: "Alive_Get",
                                consumes: @(),
                                produces: @(
                                        "application/json",
                                        "text/json",
                                        "application/xml",
                                        "text/xml"
                                ),
                                responses: {
                                        200: {
                                                description: "OK",
                                                schema: {
                                                        $ref: "#/definitions/Object"
                                                }
                                        }
                                },
                                deprecated: $False
                        }
                },
                /requirements/email-docs: {
                        get: {
                                tags: @(
                                        "CustomerRequirement"
                                ),
                                operationId: "",
                                consumes: @(),
                                produces: @(
                                        "application/json",
                                        "text/json",
                                        "application/xml",
                                        "text/xml"
                                ),
                                parameters: @(
                                        {
                                                name: "name",
                                                in: "query",
                                                required: $True,
                                                type: "integer",
                                                format: "int32"
                                        }
                                ),
                                responses: {
                                        200: {
                                                description: "OK",
                                                schema: {
                                                        type: "array",
                                                        items: {
                                                                $ref: "#/definitions/UploadResponse"
                                                        }
                                                }
                                        }
                                },
                                deprecated: $False
                        }
                }
        }
}

So I guess the properties/values where you looking for are:

所以我想你要找的属性/值是:

$Item.paths.PSObject.Properties | Select -ExpandProperty Name