将嵌套哈希 table 导出到 powershell 中的 csv
Export nested hash table to csv in powershell
我有一个数组,其中包含一个包含数据的嵌套哈希表:
tid : 1
token : 12345
participant_info : @{firstname=bob; lastname=smith; email=bob.smith@email.com}
completed : y
tid : 2
token : 67890
participant_info : @{firstname=Alice; lastname=Jones; email=alice.jones@email.com}
completed : n
我想将其输出为 CSV 文件,其中包含从内部哈希表中提取的名字、姓氏和电子邮件的嵌套项 - 例如
tid,token,firstname,surname,email,completed
1,12345,bob,smith,bob.smith@email.com,y
2,67890,alice,jones,alice.jones@email.com,n
我猜测答案是使用 foreach 循环并创建一个自定义 ps 对象,但是因为我的嵌套项没有命名,所以我不知道如何做使用此处的其他示例。
感谢任何帮助!谢谢!
鉴于您的样本:
@(
[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
我有一个数组,其中包含一个包含数据的嵌套哈希表:
tid : 1
token : 12345
participant_info : @{firstname=bob; lastname=smith; email=bob.smith@email.com}
completed : y
tid : 2
token : 67890
participant_info : @{firstname=Alice; lastname=Jones; email=alice.jones@email.com}
completed : n
我想将其输出为 CSV 文件,其中包含从内部哈希表中提取的名字、姓氏和电子邮件的嵌套项 - 例如
tid,token,firstname,surname,email,completed
1,12345,bob,smith,bob.smith@email.com,y
2,67890,alice,jones,alice.jones@email.com,n
我猜测答案是使用 foreach 循环并创建一个自定义 ps 对象,但是因为我的嵌套项没有命名,所以我不知道如何做使用此处的其他示例。
感谢任何帮助!谢谢!
鉴于您的样本:
@(
[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