3

I'm trying to ingest a JSON file into Powershell, append a block of JSON to an existing node (Components), then convert the PSCustomObject back to JSON and save the file. The JSON I'm playing with looks something like Figure 1.

As you see in my code, I run ConvertTo-Json to cast the data into a PSCustomObject, and I then append a new object to the Components node. If I view the object, $configFile in this case it all looks fine, but when I convert back to JSON the items in the Components node, are treated as strings and not evaluated into JSON (see last snippet). I imagine this is because ConvertTo-JSON treats arrays literally, but not 100% sure.

If someone can advise how to ensure the PSCustomObjects in the Components node get casted back to JSON properly I would be grateful, thank you.

Figure 1 - the original 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"
        ]
    }
} 
}

Figure 2 - my code:

#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

Figure 3 - the JSON output:

{
"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"
    }
}
}

If I increase the depth to say, 8 or beyond, the JSON comes out as follows:

{
"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"
    }
}
}
  • Hmm, just converting from JSON and converting back into JSON produces a list of serialized objects instead of JSON-syntax objects. Weird. – Vesper Aug 03 '16 at 12:07

2 Answers2

6

The ConvertTo-Json cmdlet also has a Depth parameter, beyond which an object is treated with toString() instead of going deeper with recursion. So just setting that parameter to whatever max depth of objects you have should result in a correctly formed JSON.

$configJson = ConvertTo-Json $configToPSObject -Depth 8 
# your JSON has depth of 5, get some extra
Vesper
  • 18,599
  • 6
  • 39
  • 61
  • Hi, this happens regardless of whether the -Depth param is specified. Sorry, I will update my post now. –  Aug 03 '16 at 12:42
  • Can't confirm. Anyway, "use some extra depth" approach should work. Maybe I missed something while measuring your JSON's depth. – Vesper Aug 03 '16 at 12:48
  • If I provide the -Depth parameter I get "the converted JSON string is in bad format" I can't find any issue with the formatting however as the new object is of the same type as its pre-existing siblings. –  Aug 04 '16 at 10:08
  • Again can't confirm. Please check if your original file matches what's written in the question text wise, and if you do *just* the operations listed, you receive the correct JSON. I just did like that, wrote a here-string of your provided JSON, then added an object to `EngineConfiguration.Components`, then converted the while object into JSON and received a valid JSON with all objects converted. Probably even check on another machine, maybe your Powershell/.NET installation is corrupt and affects this code. – Vesper Aug 04 '16 at 13:30
0

You have to supply the depth for the ConvertTo-Json commandlet. Otherwise it only does the first level and leaves the subnodes as is and converts them to a string apparently.

$configJson = ConvertTo-Json $obj -Depth 3
jurgenb
  • 472
  • 4
  • 11