2017-04-20 41 views
2

我有一個json對象存儲在一個變量中。從json對象中,我想獲取匹配值的節點。例如 - 其機號12項目從JSON文件中選擇一個與屬性值匹配的節點

的json看起來像

{ 
    "items": [ 
       { 
        "id": "asdasd", 
        "machineId": "12", 
        "placementGroup": "", 
        "region": "158", 
        "staticIp": "", 
        "staticIpAction": "", 
        "subnetIDs": [ 

           ] 

       }, 
       { 
        "id": "asdasd", 
        "machineId": "43", 
        "placementGroup": "", 
        "region": "158", 
        "staticIp": "", 
        "staticIpAction": "", 
        "subnetIDs": [ 

           ] 
       } 
      ] 
} 

回答

1

負載使用Get-Content cmdlet,並把它轉換使用ConvertFrom-Json cmdlet來JSON的JSON。選擇節點和由期望過濾它使用Where-Object cmdlet的設備ID

Get-Content 'yourPathToYour.json' | 
    ConvertFrom-Json | 
    Select-Object -ExpandProperty items | 
    Where-Object machineId -eq 12 

輸出:

id    : asdasd 
machineId  : 12 
placementGroup : 
region   : 158 
staticIp  : 
staticIpAction : 
subnetIDs  : {} 
+0

當心'ConvertFrom-Json'要求3的PowerShell +。 –

+0

@MarkWragg好點。 –

相關問題