I have the following object which needs to be converted to CSV file. The CSV file columns will contain all data.attrs
and data2.attrs
. data
and data2
are referenced by data. Relations
.
$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
The following code got all the attrs
in data2
for each row in data
.
$json.data |
% {
$_.relations.id |
% {
$json.data2 | ? id -eq $_ | select -ExpandProperty attrs # need to add data.attrs
}
} |
ConvertTo-Csv
Output
"x","y"
"11","1"
"12","2"
"21","1"
How to add the columns from data
in the list? There are many columns/fields in data.attr
.
Expected
"id","more",....,"x","y"
"A","A",....,"11","1"
"A","A",....,"12","2"
"B","B",....,"21","1"