在PowerShell中将其转换为json时添加方括号 [英] Add square brackets while convert it to json in powershell
本文介绍了在PowerShell中将其转换为json时添加方括号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在从TemplateJSON文件中获取参数,并从CSV文件中读取数据,然后再次将其转换为JSON。
TemplateJSON:
{
"adhocUARs":[
""
],
"rolefullpath": ""
}
CSV File:
RoleFullPath,ResourceName
FolderNameABCD,ABCD User Account Resource
我将获得此格式的输出(不带方括号):
NEWJSON:
{
"adhocUARs":"ABCD User Account Resource",
"rolefullpath": "xyz"
}
但我希望输出格式如下(带有方括号):
NEWJSON:
{
"adhocUARs":["ABCD User Account Resource"]
"rolefullpath": "xyz"
}
使用的代码:
$TemplateJSON = Convertfrom-json ([IO.File]::ReadAllText("TemplateJSON.json"))
$RoleFile = Import-CSV "CSVFile.csv" -Delimiter ','
[int]$Row = 0
Foreach ($Line in $RoleFile)
{
$Row = $Row + 1
$NewJSON = $TemplateJSON
$NewJSON.adhocUARs = $Line.ResourceName
$NewJSON.roleFullPath= $Line.RoleFullPath
$RolePath = "D:\DummyFolder"
$JSONPath = $RolePath + "patch.json"
Convertto-JSON $NewJSON | Out-file -Encoding "UTF8" $JSONPath
}
我错过了什么?GR
推荐答案
因此在JSON中[]是一个数组。 当前,您将$NewJSON.adhocUARs作为字符串单值。 简单的解决方案是:
$NewJSON.adhocUARs = @($Line.ResourceName)
这篇关于在PowerShell中将其转换为json时添加方括号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文