methuselah
methuselah

Reputation: 13206

Break down json variable into individual items in an arrays

How would you break down the following json variable into individual items in array?

[  
   {  
      "server":{  
         "name":"myUbuntuServer1",
         "imageRef":"3afe97b2-26dc-49c5-a2cc-a2fc8d80c001",
         "flavorRef":"6"
      }
   },
   {  
      "server":{  
         "name":"myUbuntuServer2",
         "imageRef":"3afe97b2-26dc-49c5-a2cc-a2fc8d80c001",
         "flavorRef":"6"
      }
   },
   {  
      "server":{  
         "name":"myUbuntuServer3",
         "imageRef":"3afe97b2-26dc-49c5-a2cc-a2fc8d80c001",
         "flavorRef":"6"
      }
   }
]

For instance, the above would translate to an array, with the following items:

Array-item 0

   {  
      "server":{  
         "name":"myUbuntuServer1",
         "imageRef":"3afe97b2-26dc-49c5-a2cc-a2fc8d80c001",
         "flavorRef":"6"
      }
   }

Array-item 1

   {  
      "server":{  
         "name":"myUbuntuServer2",
         "imageRef":"3afe97b2-26dc-49c5-a2cc-a2fc8d80c001",
         "flavorRef":"6"
      }
   }

Array-item 2

   {  
      "server":{  
         "name":"myUbuntuServer3",
         "imageRef":"3afe97b2-26dc-49c5-a2cc-a2fc8d80c001",
         "flavorRef":"6"
      }
   }

I would like to accomplish this in Powershell 2.0 and access each one individually. So far this is what I've managed to accomplish:

$jsonarr = @()
$arr = (Get-Content C:\json.json| Out-String).replace("[","") -split "(})," -replace "]",""
$jsonarr += $arr[0..1] -join ""
$jsonarr += $arr[2..3] -join ""
$jsonarr += $arr[4]

However this is extremely inflexible, and will cease to work the minute I had another server's detail to the JSON file.

Upvotes: 0

Views: 188

Answers (1)

Jaqueline Vanek
Jaqueline Vanek

Reputation: 1133

for PowerShell v2 you can use Convert between PowerShell and JSON

PS

PowerShell v3+, should be the same using the tool above:

$json = '[  
   {  
      "server":{  
         "name":"myUbuntuServer1",
         "imageRef":"3afe97b2-26dc-49c5-a2cc-a2fc8d80c001",
         "flavorRef":"6"
      }
   },
   {  
      "server":{  
         "name":"myUbuntuServer2",
         "imageRef":"3afe97b2-26dc-49c5-a2cc-a2fc8d80c001",
         "flavorRef":"6"
      }
   },
   {  
      "server":{  
         "name":"myUbuntuServer3",
         "imageRef":"3afe97b2-26dc-49c5-a2cc-a2fc8d80c001",
         "flavorRef":"6"
      }
   }
]'

$servers = ConvertFrom-Json $json

$servers.server.imageRef

returns

3afe97b2-26dc-49c5-a2cc-a2fc8d80c001
3afe97b2-26dc-49c5-a2cc-a2fc8d80c001
3afe97b2-26dc-49c5-a2cc-a2fc8d80c001

Also, don't forget "Get-Member"

PPS

PS C:\Users\joshua\Desktop> $servers.server| where name -EQ myUbuntuServer2 

name            imageRef                             flavorRef
----            --------                             ---------
myUbuntuServer2 3afe97b2-26dc-49c5-a2cc-a2fc8d80c001 6        



PS C:\Users\joshua\Desktop> $servers.server| where name -EQ myUbuntuServer2 | select -Property flavorRef

flavorRef
---------
6

PPPS

also ofcourse

$servers.server[0]

you should be able to index by name also but I'm making some silly error atm

Upvotes: 1

Related Questions