I want to join programmatically json data from a master and a details list. A simplified example of my code would be:
$master = '[{"Sample": 1.3085},{"Sample": 1.4567}]' | ConvertFrom-Json
$detail = '[{"foo":1, "bar":2},{"foo":3, "bar":4}]' | ConvertFrom-Json
$master | %{
$_ | Add-Member -MemberType NoteProperty -Name 'Detail' -Value $detail -PassThru
} | ConvertTo-Json
I expect this:
[{
"Sample": 1.3085,
"Detail": [
{"foo" :1, "bar":2},
{"foo" :3, "bar":4}
]
}
}, {
"Sample": 1.4567,
"Detail": [
{"foo" :1, "bar":2},
{"foo" :3, "bar":4}
]
}
}
]
But I get that:
[{
"Sample": 1.3085,
"Detail": {
"value": [
"@{foo=1; bar=2}",
"@{foo=3; bar=4}"
],
"Count": 2
}
}, {
"Sample": 1.4567,
"Detail": {
"value": [
"@{foo=1; bar=2}",
"@{foo=3; bar=4}"
],
"Count": 2
}
}
]
It would appear to me that the Add-Member
snippet converts the PSObject value to a string instead off taking it as-is.
Also: when the $master array contains only one element it works better, though not as expected, but if there are more than one it does the string-ified result shown above.
This is the results with one single item in $master:
{
"Sample": 1.3085,
"Detail": {
"value": [
{
"foo": 1,
"bar": 2
},
{
"foo": 3,
"bar": 4
}
],
"Count": 2
}
}
What am I doing wrong?