-
Notifications
You must be signed in to change notification settings - Fork 7.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Case insensitive ConvertFrom-Json -AsHashtable #19928
Comments
This comment was marked as resolved.
This comment was marked as resolved.
With PWSH 7.3 the result is still a case sensitive hashtable: $HashTable.FirstName
This should print an error, same as if you were to use ConvertFrom-Json without '-AsHashtable' to create a PSCustomObject. |
@UselessGuru, $Json = '{ "firstName": "Bill", "lastName": "Gates" }'
$OrderedHashTable = $Json | ConvertFrom-Json -AsHashTable
$HashTable = [HashTable]::New($OrderedHashTable, [StringComparer]::OrdinalIgnoreCase)
$HashTable.FirstName = 'Bob'
$HashTable
Name Value
---- -----
firstName Bob
lastName Gates Anyways, I see this as a workaround. It is up to the PowerShell team to come up with a better answer/solution. |
Additional thoughts:
|
I suggest reverting the 7.3 breaking change (by default hashtables are no longer case sensitive, as before 7.3) |
This results in a non-ordered hash table 👎 |
Meaning PowerShell hashtables ( $Json = '{ "firstName": "Bill", "lastName": "Gates" }'
$OrderedHashTable = $Json | ConvertFrom-Json -AsHashTable
$OrderedHashTable.PSTypeNames
System.Management.Automation.OrderedHashtable
System.Collections.Hashtable
System.Object Afaik, the new
Good point, unfortunately the new OrderedHashTable class doesn't have a |
@iSazonov, |
I'm interested in a solution for this as well! |
A way out for this issue might be a function ConvertFromJson {
param(
[Type]$As
)
if ($As.getInterfaces().Name -notcontains 'IDictionary') { Write-Error 'The type should be a dictionary type' }
# foreach (recursive) associated array iteration {
$Dicitionary = New-Object -TypeName $As # For C#, see: https://stackoverflow.com/q/752/1701026
# }
}
ConvertFromJson -As HashTable |
For what it is worth, I have created a tool set for object graphs. This module treats all (nested) dictionary classes and PowerShell objects (
or
It will find the same map node (based on the actual dictionary comparer). Besides it includes Install-Module -Name ObjectGraphTools
$Json = '{ "firstName": "Bill", "lastName": "Gates" }'
$OrderedHashTable = $Json | ConvertFrom-Json | Copy-ObjectGraph -MapAs ([Ordered]@{})
$OrderedHashTable.GetType()
IsPublic IsSerial Name BaseType
-------- -------- ---- --------
True True OrderedDictionary System.Object |
I do feel like for a lot of users including myself, having this "by design" feature makes things awkward. If you are deserializing JSON and don't care about case sensitive keys being preserved, you have to recursively update all nested hash tables to be case insensitive again which is just annoying especially when dealing with large JSON documents. I personally would be fine with bringing in a One example that has gotten really annoying is Azure function apps using PowerShell: param($EventGridEvent, $TriggerMetadata)
# Function app code
$topic = $EventGridEvent.topic Where Now I need to recursively update all nested hashtables to be case insensitive or risk having my code break trying to access a property, e.g. Now someone could tell me to just get the case right to begin with, but Microsoft breaks this all the time sending new version of event payloads with different cases and as a user I'm left in the dark and risking my code just breaking because it could not access a property. I could avoid this just having the ability to make all nested hashtables case insensitive, which now I am forced to write a recursive workaround. |
Summary of the new feature / enhancement
Since 7.3, ConvertFrom-Json -AsHashtable returns an ordered hashtable that is ALWAYS case sensitive.
Sometimes I would like to have a case insensitive ordered hashtable.
Proposed technical implementation details (optional)
2 possible solutions:
The text was updated successfully, but these errors were encountered: