2016-08-18 178 views
2

我出口從Azure的資源組的JSON的JSON文件是這樣的:轉換JSON到PowerShell的對象和轉換的PowerShell回JSON

Export-AzureRmResourceGroup -ResourceGroupName $SourceResourceGroupName -Path $filename 

然後我收到文件的JSON內容,然後它應用到一個變量:

$SourceJSON = Get-Content $filename -Raw 

然後我想打開(轉換)到這個PowerShell的對象:

$SourceJSONRG = $SourceJSON | ConvertFrom-Json 

我再看看資源從與對象:

$SourceJSONRG.resources 

storageProfile部分是空白:

enter image description here

雖然看着比較JSON($SourceJSON),該storageProfile並非空白:

enter image description here

我已經使用Format-Custom -Depth選項下井更深的嘗試:

$SourceJSONRG = $SourceJSON | ConvertFrom-Json 
$SourceJSONRG = $SourceJSONRG | Format-Custom -Depth 99 

但這放入「class PSCustomObject」無處不在,我不想。

enter image description here

我想在這裏做的最終事情是JSON轉換爲PowerShell的對象,進行更改例如更改磁盤的URI,然後轉換回JSON以使用它部署到Azure。換句話說,將JSON轉換爲PowerShell對象使我更容易使用它。

New-AzureRmResourceGroupDeployment -ResourceGroupName $TargetResourceGroupName -TemplateFile "C:\Users\marc\AppData\Local\Temp\test20692192.json" 

回答

2

我認爲這僅僅是停止顯示嵌套值的問題,但是,這並不意味着缺少值。

你可以看到,用下面的例子。給定樣本JSON字符串像在同一個「失蹤」值

$jsonString = @" 
    { 
     "root": { 
      "nested1": { 
       "nested11": { 
        "leaf1": "some value", 
        "leaf2": "some other value" 
       } 
      }, 
      "nested2": { 
       "nested22": { 
        "leaf1": "some value", 
        "leaf2": "some other value" 
       } 
      } 
     } 
    } 
"@ 
$obj = ConvertFrom-Json $jsonString 
$obj 

輸出以下並將其轉換爲一個JSON對象的結果:

root     
----     
@{nested1=; nested2=} 

但訪問和修改時的實際對象的屬性和轉換回JSON字符串,你會看到一切工程

$obj.root.nested1.nested11.leaf1 = "42" 
$jsonString = ConvertTo-Json $obj -Depth 5 
Write-Host $jsonString 

輸出:

{ 
    "root": { 
       "nested1": { 
           "nested11": { 
                "leaf1": "42", 
                "leaf2": "some other value" 
               } 
          }, 
       "nested2": { 
           "nested22": { 
                "leaf1": "some value", 
                "leaf2": "some other value" 
               } 
          } 
      } 
}