#Sanity check please: Where's this $null coming from?

10 messages · Page 1 of 1 (latest)

sudden shuttle
#

I am pulling a JSON structure via Invoke-RestMethod. No errors and powershell objects are created, so the parsing is working.

I'm pulling an array of objects that will later be iterated through, but somehow a $null is getting inserted as the first object. I am using 'Null Conditional Assignment':

# https://stackoverflow.com/a/10642435
# Null Conditional Assignment
$x = $null
$x ??= 100    # $x is now 100
$x ??= 200    # $x remains 100
# Environment
PS /> $PSVersionTable   

Name                           Value
----                           -----
PSVersion                      7.4.6
PSEdition                      Core
GitCommitId                    7.4.6
OS                             Ubuntu 24.04 LTS
Platform                       Unix
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0…}
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1
WSManStackVersion              3.0

PS />

My snippet:

$meta = Invoke-RestMethod -Uri $Uri
$thisscript = $meta.scripts | Where-Object -Property scriptid -Match $scriptid
$clusters ??= $thisscript.config.ontap_clusters
warm coral
#

you're not looking deep enough into the array in the example to show what might be expanded.

That first element of your array (the only one shown), does it match your $scriptid regex?

Does it have the properties you're expanding?

#

for example if I have this: ```ps
$arr = [PSCustomObject]@{ a = 1 }, [PSCustomObject]@{ a = 1; b = 2 }
$arr.b

I end up with an ArrayList of two elements, with the first being an explicit null.
sudden shuttle
#

I'm not sure I understand. I expect only one obhect at that level.

I walked through the logic again and found that the $scriptid wasn't defined yet

warm coral
#

object[] rather, I thought that fixed to an array list. Anyway, doesn't change the potential problem that property expansion on an array won't skip non-existent properties.

sudden shuttle
#

But I still don't understand how that null is getting added as an object in object[] when the filter is 'empty'...

#

It's working as expected as long as $scriptid is defined in the where line, but I still getting..... oh...... oh.....

warm coral
#

it matches everything in your array, I expect the first value is empty. This more closely resembles what you have in $thisscript ```ps
$thisscript = @(
[PSCustomObject]@{
scriptid = 'a'
clusters = [PSCustomObject]@{
# Does not have ontap_clusters, will emit null
}
}
[PSCustomObject]@{
scriptid = 'b'
# Does not have clusters, will not expand because of nesting
}
[PSCustomObject]@{
scriptid = 'c'
clusters = [PSCustomObject]@{
# Will expand.
ontap_clusters = 'x'
}
}
)
$thisscript.clusters.ontap_clusters | ConvertTo-Json

#

JSON conversion is just to easily show the explicit null in the array

sudden shuttle
#

Not all the $script objects have a ['config'] key defined, resulting in a list of $nulls and the two objects that has the key defined.

PS /> (($meta.scripts | Where-Object -Property scriptid -Match $empty).config).psobject
DEBUG:    1+  >>>> (($meta.scripts | Where-Object -Property scriptid -Match $empty).config).psobject
DEBUG:     ! CALL function '<ScriptBlock>'

Members             : {int Length {get;}, long LongLength {get;}, int Rank {get;}, System.Object SyncRoot {get;}…}
Properties          : {int Length {get;}, long LongLength {get;}, int Rank {get;}, System.Object SyncRoot {get;}…}
Methods             : {Get, Set, Address, get_Length…}
ImmediateBaseObject : {$null, @{files=System.Object[]}, $null, $null…}
BaseObject          : {$null, @{files=System.Object[]}, $null, $null…}
TypeNames           : {System.Object[], System.Array, System.Object}

PS />