You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Add option to ConvertFrom-Json as a Hashtable #3623
Currently ConvertFrom-Json returns a PSCustomObject. It would be nice in some cases if you could choose to return a Hashtable instead, perhaps using an -As parameter. Hashtables are often much faster for processing Json document databases compared to having to iterate through object properties.
This would also be a possible way to address #1755 .
Apart from object size and performance, it may matter with case sensitivity. I have run into issues where I couldn't use the PowerShell cmdlets because the json contained 'Property' and 'property'.
Yes, the case sensitivity is one area, empty property names are another area.
On the performance side, I would suggest that adding, removing and enumerating properties are all slower/more difficult on a pscustomobject than a hashtable.
I like the idea of an -AsHashtable option or something similar. Whenever I enumerate all the property names and then walk them, it is very inefficient. Even more so when I am recursively walking the object to turn it into a nested hashtable.
There is also not a simple way to test for a property on an object. Sometimes you can't just test for $null because the value could be $null. The existence of a property is different than a $null property value.
I also don't like using a dot property to a variable to access properties this way. A minor gripe but something that I like to avoid because at a glance it is hard to tell if that is intentional or a typo.
$object.$property
$hashtable[ $property ]
Alternatively, we could make PSCustomObject better at property enumeration, addition, and removal.
For my use case, I tend to leverage JSON for config files shared between different languages (or even module settings for certain things); I typically will have a function (let's say Export-Config) that accepts a hashtable as input and pipes to ConvertTo-Json prior to setting in a file. PSObjects and Hashtables result in the same JSON structure for my use case, so I opt to have the input as a Hashtable to save some characters and allow me to build that input value with less keystrokes.
When I want to reimport that config and make updates with a separate function (let's say Update-Config), I have to adjust the function logic and deal with Add-Member vs just stating $config["Map"] = $NewSettings. Also in agreeance with Kevin in that having $hashtable[ $property ] buried in a script is easier to identify as what it is compared to $object.$property.
Speed differences between the two are also evident when dealing with very large object arrays vs large hashtables to the point where I will almost always build a hashtable from the resulting object array for lookup speed gains compared to looking up against the object array directly.
Activity
BrucePay commentedon Apr 24, 2017
Can you provide a concrete example where hashtables are superior to an object tree?
powercode commentedon Apr 24, 2017
Apart from object size and performance, it may matter with case sensitivity. I have run into issues where I couldn't use the PowerShell cmdlets because the json contained 'Property' and 'property'.
kittholland commentedon Apr 24, 2017
Yes, the case sensitivity is one area, empty property names are another area.
On the performance side, I would suggest that adding, removing and enumerating properties are all slower/more difficult on a pscustomobject than a hashtable.
KevinMarquette commentedon Apr 25, 2017
I like the idea of an
-AsHashtableoption or something similar. Whenever I enumerate all the property names and then walk them, it is very inefficient. Even more so when I am recursively walking the object to turn it into a nested hashtable.There is also not a simple way to test for a property on an object. Sometimes you can't just test for
$nullbecause the value could be$null. The existence of a property is different than a$nullproperty value.I also don't like using a dot property to a variable to access properties this way. A minor gripe but something that I like to avoid because at a glance it is hard to tell if that is intentional or a typo.
Alternatively, we could make
PSCustomObjectbetter at property enumeration, addition, and removal.lehtoj commentedon May 13, 2017
Kevin Marquette (@KevinMarquette) That is not exactly true that there is no simple way to test for a property.
Seems simple enough?
KevinMarquette commentedon May 13, 2017
Thank you for that example. I had previously overlooked the match keyword there. I will start using that one.
scrthq commentedon Aug 3, 2017
+1 for this request.
For my use case, I tend to leverage
JSONfor config files shared between different languages (or even module settings for certain things); I typically will have a function (let's sayExport-Config) that accepts a hashtable as input and pipes to ConvertTo-Json prior to setting in a file.PSObjectsandHashtablesresult in the sameJSONstructure for my use case, so I opt to have the input as aHashtableto save some characters and allow me to build that input value with less keystrokes.When I want to reimport that config and make updates with a separate function (let's say
Update-Config), I have to adjust the function logic and deal withAdd-Membervs just stating$config["Map"] = $NewSettings. Also in agreeance with Kevin in that having$hashtable[ $property ]buried in a script is easier to identify as what it is compared to$object.$property.Speed differences between the two are also evident when dealing with very large object arrays vs large hashtables to the point where I will almost always build a hashtable from the resulting object array for lookup speed gains compared to looking up against the object array directly.
Add -AsHashtable switch to ConvertFrom-Json for issue #3623 (#5043)
iSazonov commentedon Oct 26, 2017
Leave a comment
Add -AsHashtable switch to ConvertFrom-Json for issue PowerShell#3623 (…