根据您的样本:
@(
[pscustomobject]@{
tid = 1
token = 12345
participant_info = @{
firstname = 'bob'
lastname = 'smith'
email = 'bob.smith@email.com'
}
completed = 'N'
}
...
)
和所需输出:
id,token,firstname,surname,email,completed
1,12345,bob,smith,bob.smith@email.com,y
2,67890,alice,jones,alice.jones@email.com,n
您可以这样做:
$JsonList = @( ... )
$Path = "$Env:UserProfile\file.csv"
ForEach ($Sample in $JsonList)
{
$Sample | Select-Object -Property @(
@{N = 'id'; E = { $Sample.tid }}
@{N = 'token'; E = { $Sample.token }}
@{N = 'firstname'; E = { $Sample.participant_info.firstname }}
@{N = 'surname'; E = { $Sample.participant_info.lastname }}
@{N = 'email'; E = { $Sample.participant_info.email }}
@{N = 'completed'; E = { $Sample.completed }}
) | Export-Csv -Path $Path -Append
}
编辑:您正在处理
PSCustomObject
不
Hashtable
,因此我之前的语法对此不起作用。下面是我现在假设您的代码的样子(我已经更新了上面的示例):
@"
[
{
"tid": 1,
"token": 12345,
"participant_info": {
"firstname": "bob",
"lastname": "smith",
"email": "bob.smith@email.com"
},
"completed": "N"
}
...
]
"@ | ConvertFrom-Json