2016-08-03 26 views
3

我試圖將一個JSON文件接收到Powershell中,將JSON塊附加到現有節點(組件),然後將PSCustomObject轉換回JSON並保存該文件。我玩的JSON如圖1所示。無法正確地將陣列中的PSCustomObjects轉換回JSON

正如您在我的代碼中看到的,我運行ConvertTo-Json將數據轉換爲PSCustomObject,然後將新對象追加到Components節點。如果我查看對象,$ configFile在這種情況下看起來都很好,但是當我將組件節點中的項目轉換回JSON時,將被視爲字符串並且不會評估爲JSON(請參見最後一個片段)。我想這是因爲ConvertTo-JSON從字面上看待數組,但並非100%確定。

如果有人可以建議如何確保組件節點中的PSCustomObjects正確迴歸到JSON,我將不勝感激,謝謝。

圖1 - 原JSON:

{ 
"EngineConfiguration": { 
    "PollInterval": "00:00:15", 
    "Components": [ 
     { 
      "Id": "ApplicationEventLog", 
      "FullName": "AWS.EC2.Windows.CloudWatch.EventLog.EventLogInputComponent,AWS.EC2.Windows.CloudWatch", 
      "Parameters": { 
       "LogName": "Application", 
       "Levels": "1" 
      } 
     }, 
     { 
      "Id": "SystemEventLog", 
      "FullName": "AWS.EC2.Windows.CloudWatch.EventLog.EventLogInputComponent,AWS.EC2.Windows.CloudWatch", 
      "Parameters": { 
       "LogName": "System", 
       "Levels": "7" 
      } 
     } 
    ], 
    "Flows": { 
     "Flows": 
     [ 
      "(ApplicationEventLog,SystemEventLog),CloudWatchLogs" 
     ] 
    } 
} 
} 

圖2 - 我的代碼:

#Requires -Version 3.0 

$configFile = "C:\Program Files\Amazon\EC2ConfigService\Settings\AWS.EC2.Windows.CloudWatch.json" 
$configToPSObject = ConvertFrom-Json "$(Get-Content $configFile)" 

$configToPSObject.EngineConfiguration.Components += New-Object -Type PSObject -Property ([ordered]@{ 
"Id" = "IISRequestQueueSize" 
"FullName" = "AWS.EC2.Windows.CloudWatch.PerformanceCounterComponent.PerformanceCounterInputComponent,AWS.EC2.Windows.CloudWatch" 
"Parameters" = [PSCustomObject]@{ 
     "CategoryName" = "HTTP Service Request Queues" 
     "CounterName" = "CurrentQueueSize" 
     "InstanceName" = "_Total" 
     "MetricName" = "IISRequestQueueSize" 
     "Unit" = "" 
     "DimensionName" = "" 
     "DimensionValue" = "" 
} 
}) 

$configJson = ConvertTo-Json -Depth 5 $configToPSObject 


Set-Content -Path $configFile -Value $configJson 

圖3 - 的JSON輸出:

{ 
"EngineConfiguration": { 
    "PollInterval": "00:00:15", 
    "Components": [ 
     "@{Id=ApplicationEventLog; FullName=AWS.EC2.Windows.CloudWatch.EventLog.EventLogInputComponent,AWS.EC2.Windows.CloudWatch; Parameters=}", 
     "@{Id=SystemEventLog; FullName=AWS.EC2.Windows.CloudWatch.EventLog.EventLogInputComponent,AWS.EC2.Windows.CloudWatch; Parameters=}", 
     "@{Id=IISRequestQueueSize; FullName=AWS.EC2.Windows.CloudWatch.PerformanceCounterComponent.PerformanceCounterInputComponent,AWS.EC2.Windows.CloudWatch; Parameters=}" 
     ], 
"Flows": { 
    "Flows": 
     "(ApplicationEventLog,SystemEventLog),CloudWatchLogs" 
    } 
} 
} 

如果我增加深度說,8或以上,JSON出來如下:

{ 
"EngineConfiguration": { 
    "PollInterval": "00:00:15", 
    "Components": [ 
     "@{Id=ApplicationEventLog; FullName=AWS.EC2.Windows.CloudWatch.EventLog.EventLogInputComponent,AWS.EC2.Windows.CloudWatch; Parameters=}", 
     "@{Id=SystemEventLog; FullName=AWS.EC2.Windows.CloudWatch.EventLog.EventLogInputComponent,AWS.EC2.Windows.CloudWatch; Parameters=}", 
     "Id": "IISRequestQueueSize", 
     "FullName": "AWS.EC2.Windows.CloudWatch.PerformanceCounterComponent.PerformanceCounterInputComponent,AWS.EC2.Windows.CloudWatch", 
     "Parameters": {                   
      "CategoryName": "HTTP Service Request Queues", 
      "CounterName": "CurrentQueueSize", 
      "InstanceName": "_Total",                  
      "MetricName": "IISRequestQueueSize", 
      "Unit": "", 
      "DimensionName": "", 
      "DimensionValue": "" 
     } 
    } 
], 
"Flows": { 
    "Flows": "(ApplicationEventLog,SystemEventLog),CloudWatchLogs" 
    } 
} 
} 
+0

嗯,只是從JSON轉換並轉換回JSON產生的序列化對象,而不是JSON語法對象的列表。奇怪的。 – Vesper

回答

3

ConvertTo-Json cmdlet還有一個Depth參數,超過此參數將使用toString()來處理對象,而不是使用遞歸進行更深入的處理。所以只需將該參數設置爲任何最大深度的對象都應該會導致正確形成的JSON。

$configJson = ConvertTo-Json $configToPSObject -Depth 8 
# your JSON has depth of 5, get some extra 
+0

嗨,無論是否指定了-Depth參數,都會發生這種情況。對不起,我現在會更新我的帖子。 – bleloch

+0

無法確認。無論如何,「使用一些額外的深度」的方法應該工作。也許我在測量JSON深度時錯過了一些東西。 – Vesper

+0

如果我提供-Depth參數,我會得到「已轉換的JSON字符串格式不正確」,但我無法找到任何格式問題,但由於新對象的類型與其預先存在的同級相同。 – bleloch

0

您必須提供ConvertTo-Json命令開關的深度。 否則,它只會執行第一級別並按原樣離開子節點並顯然將它們轉換爲字符串。

$configJson = ConvertTo-Json $obj -Depth 3 
+0

順便說一下,'Depth'的默認值是2,而不是1. – Vesper

+0

Ah ic,否則Components數組也不會被解析。 – n3wjack

+0

請參閱我對Vesper的評論的回覆 – bleloch