Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I avoid getting a object with nested values here?

Tags:

powershell

$BloatedObject = invoke-RestMethod "https://www.youtube.com/feeds/videos.xml?channel_id=UCegWLyW4CYzph4dYW-gYy0g"

The above object contains many properties that I don't wish to continue working with from this point forward. So I thought. I would just take the two properties that I care for and store them in new object:

$SmallObject = [PSCustomObject]@{
    Title = $BloatedObject.Title
    Link = $BloatedObject.VideoID
}

But instead of getting something akin to the output of $BloatedObject | Select-Object Title,VideoId:

title                                                                                               videoId
-----                                                                                               -------
Fast Game World Prototyping | Thomas Tobin | EPC 2023                                               Duhdhc1JEwo
KineFX: Animating Environments and Props | Mihnea Stoica | EPC 2023                                 T8al2Uxsq70
Massive Worlds – Crafting World Partition Landscapes using Houdini | Erwin Heyms | EPC 2023         baM-3JWcPJE
PDG for Games | Simon Verstraete | EPC 2023                                                         hIBnwrNHsZo
Finding Patterns – The Math Behind HEXAGONA | Christos Stavridis | EPC 2023                         nbxhnCvexdA
...

I get a nested object:

Title
-----
{Fast Game World Prototyping | Thomas Tobin | EPC 2023, KineFX: Animating Environments and Props | Mihnea Stoica | EPC 2023, Massive Worlds – Crafting World Partition Landscapes using Houdini | Erwin Heyms | EPC 2023, PDG for Games | Simon Ver…

Well, why not just go with $BloatedObject | Select-Object Title,VideoID ?

Because in between the process of moving the properties Title and VideoID to the new $smallObject, I also want to transform all the values of property VideoID:

$SmallObject = [PSCustomObject]@{
    Title = $BloatedObject.Title
    Link  = $BloatedObject.VideoID -replace "^", "https://www.youtube.com/watch?v="
}

So that $SmallObject.Link will look like:

https://www.youtube.com/watch?v=Duhdhc1JEwo
https://www.youtube.com/watch?v=T8al2Uxsq70
https://www.youtube.com/watch?v=baM-3JWcPJE
...

I have been at this for hours and I am out of tricks. Any help would be great.

like image 822
Ralf_Reddings Avatar asked Sep 13 '25 23:09

Ralf_Reddings


1 Answers

So you can just use a calculated property, seem to be exactly what you're looking for:

$BloatedObject = Invoke-RestMethod "https://www.youtube.com/feeds/videos.xml?channel_id=UCegWLyW4CYzph4dYW-gYy0g"
$smallObjects = $BloatedObject | Select-Object title, @{ N = 'link'; E = { 'https://www.youtube.com/watch?v=' + $_.VideoId }}

Or reconstruct the objects yourself:

$smallObjects = $BloatedObject | ForEach-Object {
    [pscustomobject]@{
        title = $_.title
        link  = 'https://www.youtube.com/watch?v=' + $_.VideoId
    }
}
like image 174
Santiago Squarzon Avatar answered Sep 15 '25 14:09

Santiago Squarzon