2017-08-29 3 views
-3

Ich versuche, diese JSON Inhalt durch Powershell zu analysieren:Powershell Parsen JSON

gc .\release.json | ConvertFrom-Json 

release.json wie unten ähnlichen Inhalt haben:

{ 
    "source": 2, 
    "id": 3487, 
    "environments": [ 
    { 
     "id": 11985, 
     "variables": { 
     "Var1": { "value": "val1" }, 
     "Var2": { "value": "val2" }, 
     "var3": { "value": "val3" } 
     } 
    }, 
    { 
     "id": 13797, 
     "variables": { 
     "Var1": { "value": "val1" }, 
     "Var2": { "value": "val2" }, 
     "var3": { "value": "val3" }, 
     "var4": { "value": "val4" } 
     } 
    } 
    ] 
} 

Um eine Ausgabe wie unten, kann ich viele haben Variablen in jeder Umgebung und können mehrere Umgebungen haben. Irgendwann muss ich das zu Excel bringen und analysieren.

Enter image description here

+4

Was ist Ihre Frage? –

+0

Warum ist vsts-release markiert? –

+0

@ D.J. dass Json eine exportierte Release-Definition von VSTS ist –

Antwort

1

Nicht, dass gerade nach vorne, wenn Sie durch Reihen von Wertnamen gruppieren möchten. Ich habe mir die Freiheit genommen, die Logik für dich zu denken. Im Folgenden Code gibt csv, was Sie brauchen:

$jsonContent = Get-Content .\release.json | ConvertFrom-Json; 
$environmentsArray = $jsonContent.environments; 
# Create an array of data we will be putting into Excel 
$arrData = @(); 
$columnNames = @(); 
$rowNames = @(); 


# Go through each "environments" property item in json and add "id" property to $columnNames without duplicates 
for ($i=0; $i -lt $environmentsArray.Count; $i++) { 
    [bool]$existingColumnNameFound = $false; 
    foreach($existingCol in $columnNames) { 
     if($existingCol -eq $environmentsArray[$i].id) { 
      $existingColumnNameFound = $true; 
     } 
    } 
    if($existingColumnNameFound -eq $false) { 
     $columnNames += $environmentsArray[$i].id; 
    } 

    # go through each property in environments.variables property in json and add these properties to $rowNames without duplicates 
    $environmentsArray[$i].variables.psobject.properties | foreach { 
     [bool]$existingRowNameFound = $false; 
     foreach($existingRow in $rowNames) { 
      if($existingRow -eq $_.name) { 
       $existingRowNameFound = $true; 
       break; 
      } 
     } 
     if($existingRowNameFound -eq $false) { 
      $rowNames += $_.name; 
     } 
    } 

} 

foreach($existingRow in $rowNames) { 
    $objRowItem = New-Object System.Object; 
    $objRowItem | Add-Member -MemberType NoteProperty -Name "ValueName" -Value $existingRow; 
    # Create all columns for each row object 
    foreach($existingCol in $columnNames) { 
     $objRowItem | Add-Member -MemberType NoteProperty -Name $existingCol -Value ""; 
    } 
    foreach($existingCol in $columnNames) { 

     # Populate the column in row object we are adding to $arrData 
     for ($i=0; $i -lt $environmentsArray.Count; $i++) { 
      $environmentsArray[$i].variables.psobject.properties | foreach { 
       # If json data "id" property and the value property name equal, add value to column 
       if(($_.name -eq $objRowItem.ValueName) -and ($existingCol.ToString() -eq $environmentsArray[$i].id.ToString())) { 
        $objRowItem.$existingCol = $_.value.value; 
       } 

      } 
     } 
    } 
    # Add this object containing columns to $arrData 
    $arrData += $objRowItem; 
} 

# Convert this data to CSV 
$arrData | ConvertTo-Csv -NoTypeInformation -Delimiter "," | % {$_ -replace '"',''} | Out-File .\output.csv 

Ergebnis: enter image description here