Skip to content

Hashtables resulting from ConvertFrom-JSON -AsHashtable have case sensitive keys #6928

Closed

Description

Steps to reproduce

@{"test"=1}.containsKey("Test")
True

("{""test"":1}" | convertfrom-json -AsHashtable).containsKey("Test")
False

Expected behavior

both should return true and keys should not be case sensitive

Actual behavior

keys from convertfrom-json -ashashtable are case sensitive

Environment data

> $PSVersionTable
PSVersion                      6.1.0-preview.1
PSEdition                      Core
GitCommitId                    v6.1.0-preview.1
OS                             Microsoft Windows 10.0.17666
Platform                       Win32NT
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1
WSManStackVersion              3.0

Activity

  1. markekraus commented on May 24, 2018

    @markekraus
    Contributor

    Bill Easton (@strawgate) Case sensitivity is by design to accommodate case sensitive key names:

    {
      "key1": "value1",
      "KEY1": "value2"
    }

    This is documented in -AsHashtable fdocumentation:

    https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/convertfrom-json?view=powershell-6#optional-parameters

    If the JSON contains a list with keys that only differ in casing. Without the switch, those keys would be seen as identical keys and therefore only the last one would get used.

  2. strawgate commented on May 24, 2018

    @strawgate
    ContributorAuthor

    Case sensitive hashtables are not the norm elsewhere --

    group-object -AsHashTable does not produce case sensitive hashtables and neither does @{} with group-object being the reference for justifying the behavior and flag that was used.

    I guess i'll leave this as a fun warning to anyone else using this cmdlet

    $test1 = @{"Test"=1} | convertto-json | convertfrom-json -ashashtable
    $test1.containsKey("test") # False
    
    $test2 = @{"Test"=1}
    $test2.containskey("test") # True
    
  3. markekraus commented on May 24, 2018

    @markekraus
    Contributor

    Case sensitive hashtables are not the norm elsewhere

    Yes, but -AsHashTable on convertfrom-json was implemented specifically to address the issue of case sensitive key names among other issues. It's by design and documented as such.

    It probably should have been implemented as case insensitive by default with an option for case sensitive.

  4. strawgate commented on May 25, 2018

    @strawgate
    ContributorAuthor

    Referencing the original PR: #5043

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions