Skip to content

ConvertFrom-Json does not handles same keys with different case (but JSON is case sensitive) #3705

@ghost

Description

Steps to reproduce

$data = @"
{
"firstName": "Bill",
"Firstname": "Bob",
"lastName": "Gates"
}
"@

$json = $data | ConvertFrom-Json

Expected behavior

Convert JSON data, containing 3 pairs of key/value, with same key name "firstName" but with a different case:

  • firstName: with the letter "f" in lowercase and the letter "N" in upper case
  • Firstname: with the letter "F" in uppercase and the letter "n" in lowercase

Actual behavior

Exception thrown:

ConvertFrom-Json : Cannot convert the JSON string because a dictionary that was converted from the string contains the duplicated keys 'firstName' and 'Firstname'.
At line:8 char:17
$json = $data | ConvertFrom-Json

 CategoryInfo          : InvalidOperation: (:) [ConvertFrom-Json], InvalidOperationException
 FullyQualifiedErrorId : DuplicateKeysInJsonString,Microsoft.PowerShell.Commands.ConvertFromJsonCommand

As PowerShell is case insensitive, it considers the second key "Firstname", with a different case, as a duplicate key.
JSON allows to have same key name but with different case

Environment data

Name Value


PSVersion 5.1.14393.1066
PSEdition Desktop
PSCompatibleVersions {1.0, 2.0, 3.0, 4.0...}
BuildVersion 10.0.14393.1066
CLRVersion 4.0.30319.42000
WSManStackVersion 3.0
PSRemotingProtocolVersion 2.3
SerializationVersion 1.1.0.1

Metadata

Metadata

Assignees

No one assigned

    Labels

    Issue-BugIssue has been identified as a bug in the productResolution-AnsweredThe question is answered.WG-Cmdlets-Utilitycmdlets in the Microsoft.PowerShell.Utility module

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions