[英]Adding square brackets in json after split and convert from CSV in PowerShell
我有一个场景,我必须在一定数量的行中拆分 csv,并且对于每个批次,应该有通过 PowerShell 脚本生成的 json 文件。
这是我目前正在做的事情:
$csv = "C:\Desktop\report.csv"
[int]$totalRows = 0
$reader = New-Object IO.StreamReader $csv
while($reader.ReadLine() -ne $null) { $totalRows++ }
$reader.Dispose()
$totalRows
$startRow = 0
$counter = 1
while($startRow -lt $totalRows)
{
Import-CSV $csv | Select-Object @{expression = { "Append this value"+ $_.Name}; label = 'NewName'}, @{expression = {$_.Account}; label = 'AccountNumber'} | Select-Object -skip $startRow -first 2 | ConvertTo-Json | Add-Content -Path "C:\Desktop\r_$($counter).json"
$startRow += 2
$counter++
}
这里唯一的问题是我无法将帐号值括在方括号 [] 中:
实际: "AccountNumber": "123"
预期: "AccountNumber": ["123"]
此外,我不确定如何通过 this 将整个 json 放在根元素下的每个文件中。 也不确定这个“ConvertTo-Json”是否是需要编辑的csv数据的方法,请帮忙。
这是一个 csv 供参考 -
Name,Account,Role
John,123,Second
Rocky,345,Third
Tony,234,First
Rocky,345,Second
Matt,999,Second
Bernard,888,Third
Matt,999,First
Jacob,789,Second
Angela,777,Second
Jacob,789,First
预期产出
第一个文件:
{
"details":
[
{
"NewName": "Append this valueJohn",
"AccountNumber": ["123","333"]
},
{
"NewName": "Append this valueRocky",
"AccountNumber": ["345"]
}
]
}
第二个文件:
{
"details":
[
{
"NewName": "Append this valueTony",
"AccountNumber": ["234"]
},
{
"NewName": "Append this valueRocky",
"AccountNumber": ["345"]
}
]
}
所以直到第 6 个文件:
{
"details":
[
{
"NewName": "Append this valueAngela",
"AccountNumber": ["777"]
},
{
"NewName": "Append this valueJacob",
"AccountNumber": ["789"]
}
]
}
Thanks
继续我的评论,因为我不能在那里使用换行符:
将以下 PowerShell 对象转换为Json
(JavaScript Object Notation) :
@{
details = @{
NewName = 'Append this valueJohn'
AccountNumber = 123
}, @{
NewName = 'Append this valueRocky'
AccountNumber = 345
}
} |ConvertTo-Json
结果是
{
"details": [
{
"NewName": "Append this valueJohn",
"AccountNumber": 123
},
{
"NewName": "Append this valueRocky",
"AccountNumber": 345
}
]
}
如果要引用这些值,则需要将它们强制为字符串
(通过使用引号或例如[String]
初始值设定项):
@{
details = @{
NewName = 'Append this valueJohn'
AccountNumber = "123"
}, @{
NewName = 'Append this valueRocky'
AccountNumber = [String]345
}
} |ConvertTo-Json
结果:
{
"details": [
{
"NewName": "Append this valueJohn",
"AccountNumber": "123"
},
{
"NewName": "Append this valueRocky",
"AccountNumber": "345"
}
]
}
如果您还想将单个项目(标量)字符串放在方括号(代表数组)之间,则需要使用数组子表达式运算符@( )
或逗号运算符,
将值强制为数组。 请注意,当涉及多个值时,这会自动发生,例如: AccountNumber = "123", "333"
@{
details = @{
NewName = 'Append this valueJohn'
AccountNumber = @("123")
}, @{
NewName = 'Append this valueRocky'
AccountNumber = ,[String]345
}
} |ConvertTo-Json -Depth 9
(还要注意-Depth
参数)
结果:
{
"details": [
{
"NewName": "Append this valueJohn",
"AccountNumber": [
"123"
]
},
{
"NewName": "Append this valueRocky",
"AccountNumber": [
"345"
]
}
]
}
请注意,方括号与默认情况下的ConvertTo-Json
扩展输出不在同一行。 您可能会考虑使用-Compress
参数,但这会将所有内容放在同一行上:
{"details":[{"NewName":"Append this valueJohn","AccountNumber":["123"]},{"NewName":"Append this valueRocky","AccountNumber":["345"]}]}
换句话说,即使外观不同,在技术上都是相同的,并且代表同一个对象。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.