none
Break down json file into three arrays RRS feed

  • Question

  • 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.

    Tuesday, July 14, 2015 12:05 AM

Answers

  • True, this would handle it better - 

    $jsonarr = @()
    $arr = (Get-Content C:\json.json| Out-String).replace("[","") -split "(})," -replace "]",""
    $count = $arr.count
    for($y=0; $y -lt $count; $y++){
    
    $jsonarr += $arr[$y..($y+1)] -join ""
    $y++
    
    
    }
    

    Tuesday, July 14, 2015 1:12 PM

All replies

  • It's a little bit messy but it should work - 

    $jsonarr = @()
    $arr = (gc C:\json.json -Raw).replace("[","") -split "(})," -replace "]",""
    $jsonarr += $arr[0..1] -join ""
    $jsonarr += $arr[2..3] -join ""
    $jsonarr += $arr[4]

    Tuesday, July 14, 2015 9:27 AM
  • Raw parameter will not work in Powershell 2.0 .Only correction is :
    $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]

    Tuesday, July 14, 2015 10:44 AM
  • Raw parameter will not work in Powershell 2.0 .Only correction is :
    $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]


    Hi, this appears to be a tad inflexible. What happens when an additional server is added to the JSON file. Would it not be picked up at all?
    Tuesday, July 14, 2015 12:51 PM
  • True, this would handle it better - 

    $jsonarr = @()
    $arr = (Get-Content C:\json.json| Out-String).replace("[","") -split "(})," -replace "]",""
    $count = $arr.count
    for($y=0; $y -lt $count; $y++){
    
    $jsonarr += $arr[$y..($y+1)] -join ""
    $y++
    
    
    }
    

    Tuesday, July 14, 2015 1:12 PM
  • Fantastic this works great!
    Thursday, July 16, 2015 8:06 PM