The following code joins data
and data2
and creates a denormalized CSV file.
$str = '{
"data": [
{ "attrs": { "id": "A", "more": "A" },
"relations": [{"id": "r11"}, {"id": "r12"}] },
{ "attrs": { "id": "B", "more": "B" },
"relations": [{"id": "r21"}] }
],
"data2": [
{"id": "r11", "attrs": { "x": "11", "y": "1"}},
{"id": "r12", "attrs": { "x": "12", "y": "2"}},
{"id": "r21", "attrs": { "x": "21", "y": "1"}}
]}'
$json = $str | ConvertFrom-Json
$json.data |
% {
#$data = $null
$data = $_.attrs
$_.relations.id | #select -first 1 |
% {
$d2 = $json.data2 | ? id -eq $_ | select -ExpandProperty attrs
Write-Host -ForegroundColor Green $d2
$d2 | Get-Member -Type Properties | % {
Write-Host -ForegroundColor Red $_
Add-Member -InputObject $data -NotePropertyName $_.Name -NotePropertyValue $_
}
$data
}
} |
ConvertTo-Csv
However, it got the following error?
Add-Member:
Line |
12 | Add-Member -InputObject $data -NotePropertyName $_.Name - …
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
| Cannot add a member with the name "x" because a member with that name already exists. To overwrite the member anyway, add the Force parameter to your command.
string y=1