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"
    }
}
}
1
  • Hmm, just converting from JSON and converting back into JSON produces a list of serialized objects instead of JSON-syntax objects. Weird. Commented Aug 3, 2016 at 12:07

2 Answers 2

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
Sign up to request clarification or add additional context in comments.

4 Comments

Hi, this happens regardless of whether the -Depth param is specified. Sorry, I will update my post now.
Can't confirm. Anyway, "use some extra depth" approach should work. Maybe I missed something while measuring your JSON's depth.
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.
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.
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

3 Comments

By the way, the default value for Depth is 2, not 1.
Ah ic, otherwise the Components array would not have been parsed either.
Please see my response to Vesper's comment

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.