无法将数组内的PSCustomObjects正确转换回JSON [英] Cannot convert PSCustomObjects within array back to JSON correctly

查看:70
本文介绍了无法将数组内的PSCustomObjects正确转换回JSON的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将JSON文件提取到Powershell中,将JSON块附加到现有节点(组件),然后将PSCustomObject转换回JSON并保存文件.我正在使用的JSON类似于图1.

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.

正如您在我的代码中看到的那样,我运行ConvertTo-Json将数据转换为PSCustomObject,然后将新对象附加到Components节点.如果查看该对象,在这种情况下,$ configFile看起来一切正常,但是当我将其转换回JSON时,Components节点中的项目将被视为字符串,而不是被评估为JSON(请参阅最后一个片段).我想这是因为ConvertTo-JSON会按字面意义处理数组,但不能100%确定.

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.

如果有人可以建议如何确保Components节点中的PSCustomObjects被正确地转换回JSON,我将不胜感激,谢谢.

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.

图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就会显示如下:

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

推荐答案

ConvertTo-Json cmdlet还具有 Depth 参数,超出此范围的对象将使用toString()进行处理,而不是通过递归进行深入处理.因此,只要将该参数设置为您所拥有的对象的最大深度,就可以生成正确格式的JSON.

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

这篇关于无法将数组内的PSCustomObjects正确转换回JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆