简体   繁体   English

从Invoke-RestMethod访问JSON中的数据

[英]Accessing Data in JSON from Invoke-RestMethod

I'm accessing a REST API from an Elasticsearch Cluster from Powershell. 我正在从Powershell的Elasticsearch群集访问REST API。 Invoking the Get call and receiving the data is not an issue. 调用Get调用并接收数据不是问题。 Normally I can get the desired data without an issues but I'm struggling with this particular response. 通常,我可以毫无问题地获取所需的数据,但是我正在为这种特殊的响应而苦苦挣扎。 Me messing up somehow. 我搞砸了。

So the call is a get to the API and I have the response in a variable ($r). 因此,该调用是对API的获取,我的响应包含在变量($ r)中。

When using Postman the JSON response looks like this (only the first two nodes in the response shown. In the actual response there are 10 nodes): 使用Postman时,JSON响应如下所示(仅显示响应中的前两个节点。实际响应中有10个节点):

{
    "_nodes": {
        "total": 10,
        "successful": 10,
        "failed": 0
    },
    "cluster_name": "helios",
    "nodes": {
        "LvrIjMcFR1ahhFOnPl-gsQ": {
            "name": "log001",
            "transport_address": "172.16.1.36:9300",
            "host": "172.16.1.36",
            "ip": "172.16.1.36",
            "version": "6.2.4",
            "build_hash": "ccec39f",
            "total_indexing_buffer": 1601889894,
            "roles": [
                "master",
                "data",
                "ingest"
            ],
            "attributes": {
                "ml.machine_memory": "68718481408",
                "ml.max_open_jobs": "20",
                "box_type": "hot",
                "ml.enabled": "true"
            }
        },
        "yuW4R6yyRFKYdNDcrnEbXA": {
            "name": "log008",
            "transport_address": "172.16.1.63:9300",
            "host": "172.16.1.63",
            "ip": "172.16.1.63",
            "version": "6.2.4",
            "build_hash": "ccec39f",
            "total_indexing_buffer": 1603639705,
            "roles": [
                "data",
                "ingest"
            ],
            "attributes": {
                "ml.machine_memory": "68718481408",
                "ml.max_open_jobs": "20",
                "box_type": "warm",
                "ml.enabled": "true"
            }
        }
    }
}

So I can use 所以我可以用

$r.nodes

to get the nodes 得到节点

LvrIjMcFR1ahhFOnPl-gsQ : @{name=log001; transport_address=172.16.1.36:9300; host=172.16.1.36; ip=172.16.1.36;
                         version=6.2.4; build_hash=ccec39f; total_indexing_buffer=1601889894; roles=System.Object[];
                         attributes=; settings=; os=; process=; jvm=; thread_pool=; transport=; http=;
                         plugins=System.Object[]; modules=System.Object[]; ingest=}
6QHP2i8rTXyw_z2jKqXnaA : @{name=log004; transport_address=172.16.1.68:9300; host=172.16.1.68; ip=172.16.1.68;
                         version=6.2.4; build_hash=ccec39f; total_indexing_buffer=1603639705; roles=System.Object[];
                         attributes=; settings=; os=; process=; jvm=; thread_pool=; transport=; http=;
                         plugins=System.Object[]; modules=System.Object[]; ingest=}
DAgkSb6gQQauZlMDaA-VCw : @{name=log002; transport_address=172.16.1.49:9300; host=172.16.1.49; ip=172.16.1.49;
                         version=6.2.4; build_hash=ccec39f; total_indexing_buffer=1601889894; roles=System.Object[];
                         attributes=; settings=; os=; process=; jvm=; thread_pool=; transport=; http=;
                         plugins=System.Object[]; modules=System.Object[]; ingest=}
aVu1OKHoRSinIZ-Ggf1eFg : @{name=log010; transport_address=172.16.1.60:9300; host=172.16.1.60; ip=172.16.1.60;
                         version=6.2.4; build_hash=ccec39f; total_indexing_buffer=1603639705; roles=System.Object[];
                         attributes=; settings=; os=; process=; jvm=; thread_pool=; transport=; http=;
                         plugins=System.Object[]; modules=System.Object[]; ingest=}
6i9_HsVjTH29r0_vnnIvHg : @{name=log006; transport_address=172.16.1.67:9300; host=172.16.1.67; ip=172.16.1.67;
                         version=6.2.4; build_hash=ccec39f; total_indexing_buffer=1603639705; roles=System.Object[];
                         attributes=; settings=; os=; process=; jvm=; thread_pool=; transport=; http=;
                         plugins=System.Object[]; modules=System.Object[]; ingest=}
QN7vh_UZTg2lgMJ_bZOcFw : @{name=log003; transport_address=172.16.1.35:9300; host=172.16.1.35; ip=172.16.1.35;
                         version=6.2.4; build_hash=ccec39f; total_indexing_buffer=1601889894; roles=System.Object[];
                         attributes=; settings=; os=; process=; jvm=; thread_pool=; transport=; http=;
                         plugins=System.Object[]; modules=System.Object[]; ingest=}
C6iDoIA7RkaM8-OXwG49TQ : @{name=log009; transport_address=172.16.1.62:9300; host=172.16.1.62; ip=172.16.1.62;
                         version=6.2.4; build_hash=ccec39f; total_indexing_buffer=1603639705; roles=System.Object[];
                         attributes=; settings=; os=; process=; jvm=; thread_pool=; transport=; http=;
                         plugins=System.Object[]; modules=System.Object[]; ingest=}
gEJx2P5uRmClV7R_oOyo2w : @{name=log005; transport_address=172.16.1.73:9300; host=172.16.1.73; ip=172.16.1.73;
                         version=6.2.4; build_hash=ccec39f; total_indexing_buffer=1603639705; roles=System.Object[];
                         attributes=; settings=; os=; process=; jvm=; thread_pool=; transport=; http=;
                         plugins=System.Object[]; modules=System.Object[]; ingest=}
yuW4R6yyRFKYdNDcrnEbXA : @{name=log008; transport_address=172.16.1.63:9300; host=172.16.1.63; ip=172.16.1.63;
                         version=6.2.4; build_hash=ccec39f; total_indexing_buffer=1603639705; roles=System.Object[];
                         attributes=; settings=; os=; process=; jvm=; thread_pool=; transport=; http=;
                         plugins=System.Object[]; modules=System.Object[]; ingest=}
HLQquAaoQaedjtwvATf2cg : @{name=log007; transport_address=172.16.1.69:9300; host=172.16.1.69; ip=172.16.1.69;
                         version=6.2.4; build_hash=ccec39f; total_indexing_buffer=1603639705; roles=System.Object[];
                         attributes=; settings=; os=; process=; jvm=; thread_pool=; transport=; http=;
                         plugins=System.Object[]; modules=System.Object[]; ingest=}

I then expected to do 然后我期望做

$r.nodes[0]

to get the first node but this gives me the exact same as $r.nodes before. 获取第一个节点,但这与之前的$ r.nodes完全相同。

$r.nodes."LvrIjMcFR1ahhFOnPl-gsQ"

then gives me the info I need about that node: 然后给我我需要的有关该节点的信息:

name                  : log001
transport_address     : 172.16.1.36:9300
host                  : 172.16.1.36
ip                    : 172.16.1.36
version               : 6.2.4
build_hash            : ccec39f
total_indexing_buffer : 1601889894
roles                 : {master, data, ingest}
attributes            : @{ml.machine_memory=68718481408; ml.max_open_jobs=20; box_type=hot; ml.enabled=true}

But how can I get some sort of iterator over the nodes as I do not know the ID of the nodes beforehand? 但是,由于我事先不知道节点的ID,如何在节点上获得某种迭代器?

I'm using Powershell 5.1 我正在使用Powershell 5.1

You can use the PSObject property to break down the object. 您可以使用PSObject属性分解对象。 Then pipe till you get Value 然后管道直到您获得价值

$II = @"
{
    "_nodes": {
        "total": 10,
        "successful": 10,
        "failed": 0
    },
    "cluster_name": "helios",
    "nodes": {
        "LvrIjMcFR1ahhFOnPl-gsQ": {
            "name": "log001",
            "transport_address": "172.16.1.36:9300",
            "host": "172.16.1.36",
            "ip": "172.16.1.36",
            "version": "6.2.4",
            "build_hash": "ccec39f",
            "total_indexing_buffer": 1601889894,
            "roles": [
                "master",
                "data",
                "ingest"
            ],
            "attributes": {
                "ml.machine_memory": "68718481408",
                "ml.max_open_jobs": "20",
                "box_type": "hot",
                "ml.enabled": "true"
            }
        },
        "yuW4R6yyRFKYdNDcrnEbXA": {
            "name": "log008",
            "transport_address": "172.16.1.63:9300",
            "host": "172.16.1.63",
            "ip": "172.16.1.63",
            "version": "6.2.4",
            "build_hash": "ccec39f",
            "total_indexing_buffer": 1603639705,
            "roles": [
                "data",
                "ingest"
            ],
            "attributes": {
                "ml.machine_memory": "68718481408",
                "ml.max_open_jobs": "20",
                "box_type": "warm",
                "ml.enabled": "true"
            }
        }
    }
}
"@ | ConvertFrom-Json

$II.nodes.PSobject.Properties | %{
    $_.Value
}

The problem here is that the json is converted to a PSCustomObject. 这里的问题是将json转换为PSCustomObject。 Probably the easiest way to enumerate the keys is to do something like: 枚举键的最简单方法可能是执行以下操作:

$NodeNames = $r.Nodes | Get-Member -MemberType NoteProperty | Select-Object -ExpandProperty Name
foreach ($NodeName in $NodeNames) {
    $r.Nodes.$Nodename
}

If you were to use PowerShell Core 6.1, you could also do something like: 如果要使用PowerShell Core 6.1,还可以执行以下操作:

$r = (Invoke-WebRequest 'http://url').Content | ConvertFrom-Json -AsHashTable

And then you could enumerate through the keys with: 然后,您可以使用以下命令枚举键:

foreach ($node in $r.Nodes.GetEnumerator()) {
    #code here
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM