I would like to print the name
of those packages that have a child whose packageDescription
is 'ABC'.
As you can see from my output, I am seeing the package descriptions inside a data structure, but I do not know how I can access those values from my pipeline, so there is currently no filtering on package descriptions. The desired output is a simple list of names.
The JSON, which is from a universal package feed and has been shortened a lot:
{
"count": 62,
"value": [
{
"id": "61979cf2-4ad4-4d6c-b8c7-b67787c4874d",
"name": "pack-de",
"versions": [
{
"id": "760870c0-5fc5-4453-b7fa-4648cc99bfcb",
"version": "1.15.0-344001",
"isLatest": true,
"packageDescription": "ABC",
"publishDate": "2023-04-04T14:14:57.5035842Z"
}
],
"_links": {
"self": {
"href": ""
},
"feed": {
"href": ""
},
"versions": {
"href": ""
}
}
},
{
"id": "fea11928-decb-4334-89b6-89915a3e07b9",
"name": "pack-en",
"versions": [
{
"id": "02a839a1-0d4c-403f-a6c8-ac6e00dec723",
"version": "1.15.0-344001",
"isLatest": true,
"packageDescription": "ABC",
"publishDate": "2023-04-04T14:15:09.1276991Z"
}
],
"_links": {
"self": {
"href": ""
},
"feed": {
"href": ""
},
"versions": {
"href": ""
}
}
},
{
"id": "a5bb64b7-37c5-4ef4-ae0e-5e95a2a85ae3",
"name": "pack-zh-cn",
"versions": [
{
"id": "79dee41e-824f-41e5-aa4b-b7126adb8053",
"version": "1.13.2-343998",
"isLatest": true,
"packageDescription": "AZ",
"publishDate": "2023-04-04T14:11:00.2381001Z"
}
],
"_links": {
"self": {
"href": ""
},
"feed": {
"href": ""
},
"versions": {
"href": ""
}
}
}
]
}
My PowerShell
$PackageInfo = ConvertFrom-Json -inputObject $myJson
$description = "ABC"
$PackageInfo.value | select name, versions, @{Name = $description; Expression = { ($_.versions.packageDescription | Where-Object $_.packageDescription -eq $description) }}
The current output I get:
name versions ABC
---- -------- ---
pack-de {@{id=760870c0-5fc5-4453-b7fa-4648cc99bfcb; version=1.15.0-344001; isLatest=True; packageDescription=ABC; publishDate=2023-04-04T14:14:57.5035842Z}}
pack-en {@{id=02a839a1-0d4c-403f-a6c8-ac6e00dec723; version=1.15.0-344001; isLatest=True; packageDescription=ABC; publishDate=2023-04-04T14:15:09.1276991Z}}
pack-zh-cn {@{id=79dee41e-824f-41e5-aa4b-b7126adb8053; version=1.13.2-343998; isLatest=True; packageDescription=AZ; publishDate=2023-04-04T14:11:00.2381001Z}}
Seems like you're looking for filtering instead of selecting, for that you can use Where-Object
:
$description = 'ABC'
(ConvertFrom-Json -InputObject $myJson).value |
Where-Object { $_.versions.packageDescription -eq $description } |
ForEach-Object name
With the Json in question this would result in:
pack-de
pack-en
If you want to get objects out of it instead of values, you can use Select-Object
but you need to filter beforehand:
$description = 'ABC'
(ConvertFrom-Json -InputObject $myJson).value |
Where-Object { $_.versions.packageDescription -eq $description } |
Select-Object name, @{ N='packageDescription'; E={ $_.versions.packageDescription }}
Which results in:
name packageDescription
---- ------------------
pack-de ABC
pack-en ABC