gpt4 book ai didi

json - 使用 powershell 编辑 .json 文件

转载 作者:可可西里 更新时间:2023-11-01 11:46:23 26 4
gpt4 key购买 nike

我有一个 .json 文件需要在用户数据中进行编辑,因此我必须使用 powershell 来完成此操作。json 看起来像这样:

{
"EngineConfiguration": {
"PollInterval": "00:00:15",
"Components": [
{
"Id": "CustomLogs",
"FullName": "AWS.EC2.Windows.CloudWatch.CustomLog.CustomLogInputComponent,AWS.EC2.Windows.CloudWatch",
"Parameters": {
"LogDirectoryPath": "C:\\CustomLogs\\",
"TimestampFormat": "MM/dd/yyyy HH:mm:ss",
"Encoding": "UTF-8",
"Filter": "",
"CultureName": "en-US",
"TimeZoneKind": "Local"
}
}
],
"Flows": {
"Flows":
[
"(ApplicationEventLog,SystemEventLog),CloudWatchLogs"
]
}
}
}

我希望它看起来像这样--

{
"EngineConfiguration": {
"PollInterval": "00:00:15",
"Components": [
{
"Id": "CustomLogs",
"FullName": "AWS.EC2.Windows.CloudWatch.CustomLog.CustomLogInputComponent,AWS.EC2.Windows.CloudWatch",
"Parameters": {
"LogDirectoryPath": "C:\\ProgramData\\Amazon\\CodeDeploy\\deployment-logs",
"TimestampFormat": "[yyyy-MM-dd HH:mm:ss.fff]",
"Encoding": "UTF-8",
"Filter": "",
"CultureName": "en-US",
"TimeZoneKind": "Local"
}
}
],
"Flows": {
"Flows":
[
"(ApplicationEventLog,SystemEventLog, CustomLogs),CloudWatchLogs"
]
}
}
}

在自定义日志参数中,LogDirectoryPath 和 TimestampFormat 都已更改。此外,在流程部分,我已将“CustomLogs”添加到 CloudWatch 组。

我试着让它与这样的代码一起工作:

$a = Get-Content 'C:\PATH\TO\file.json' -raw | ConvertFrom-Json
$a.EngineConfiguration.Components[0].Parameters = '{"LogDirectoryPath": "","TimestampFormat": "[yyyy-MM-dd HH:mm:ss.fff]","Encoding": "UTF-8","Filter": "","CultureName": "en-US","TimeZoneKind": "Local"}'
$a | ConvertTo-Json | set-content 'C:\PATH\TO\output.json'

但这会产生非常难看的输出

{
"EngineConfiguration": {
"PollInterval": "00:00:15",
"Components": [
"@{Id=CustomLogs; FullName=AWS.EC2.Windows.CloudWatch.CustomLog.CustomLogInputComponent,AWS.EC2.Windows.CloudWatch; Parameters={\"LogDirectoryPath\": \"\",\"TimestampFormat\": \"[yyyy-MM-dd HH:mm:ss.fff]\",\"Encoding\": \"UTF-8\",\"Filter\": \"\",\"CultureName\": \"en-US\",\"TimeZoneKind\": \"Local\"}}",
"@{Id=CloudWatchLogs; FullName=AWS.EC2.Windows.CloudWatch.CloudWatchLogsOutput,AWS.EC2.Windows.CloudWatch; Parameters=}"
],
"Flows": {
"Flows": "(ApplicationEventLog,SystemEventLog),CloudWatchLogs"
}
}
}

有没有更优雅的方法来做到这一点?任何建议将不胜感激。谢谢!

最佳答案

尝试为 ConvertTo-Json 使用 -Depth 开关。默认情况下,这会将深度超过 2 的任何子元素压缩为您所看到的对象的字符串表示形式:

"@{Id=CustomLogs; etc."

通过指定更深的深度,您可以获得更像您想要的格式。将它与压缩过多空白的东西结合起来:

((ConvertFrom-Json $a) | ConvertTo-Json -Depth 4) -replace ((" "*4)," ")

关于json - 使用 powershell 编辑 .json 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43638102/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com