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

查看:24
本文介绍了无法将数组中的 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 时,组件节点中的项目被视为字符串,而不是评估为 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.

如果有人可以建议如何确保组件节点中的 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:

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

图 2 - 我的代码:

Figure 2 - my code:

#Requires -Version 3.0

$configFile = "C:Program FilesAmazonEC2ConfigServiceSettingsAWS.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 输出:

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

如果我增加深度说,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天全站免登陆