I am looking for a way to filter nested objects with PowerShell. Let's say I have a list that contains UPN addresses.
$UPN = "[email protected]", "[email protected]"
In the below object converted to JSON, I want to get only those objects where "Attr1" within whole object contains any address from the above list.
[
{
"Id": "1",
"Name": "XYZ",
"Attr1": [
{
"Id": "1",
"UPN": "[email protected]"
},
{
"Id": "2",
"UPN": "[email protected]"
},
{
"Id": "3",
"UPN": "[email protected]"
}
],
"Attr2": [
{
"Id": "1",
[...]
}
],
"Attr3": [
{
"Id": "1",
[...]
},
{
"Id": "2",
[...]
}
]
},
{
"Id": "2",
"Name": "XYZ",
"Attr1": [
{
"Id": "1",
"UPN": "[email protected]"
}
],
"Attr2": [
],
"Attr3": [
{
"Id": "1",
[...]
}
]
}
]
How to filter such an object? The standard method to not work for me (I am not getting the desired result, losing data):
$CollectionOfObjects | Where-Object {$_.Attr1.UPN -in $UPN}
How to handle this?
Regards!