Private/ConvertFrom-JsonCaseSensitive.ps1
|
<# .SYNOPSIS Parse JSON into case-sensitive dictionaries, tolerating keys that differ only in casing. .DESCRIPTION ConvertFrom-Json rejects JSON objects whose keys differ only in casing on both editions. This helper parses such JSON into case-sensitive dictionaries instead: on PowerShell 7 via ConvertFrom-Json -AsHashtable, on Windows PowerShell 5.1 via System.Web.Script.Serialization.JavaScriptSerializer, which -AsHashtable does not exist on. Pipe the result through ConvertTo-PsObject to restore the usual PSCustomObject shape. .PARAMETER Json The JSON text to parse. .EXAMPLE PS> ConvertFrom-JsonCaseSensitive -Json $Response.Content | ConvertTo-PsObject #> function ConvertFrom-JsonCaseSensitive { [CmdletBinding()] param ( [Parameter(Mandatory)] [AllowEmptyString()] [string]$Json ) if ($PSVersionTable.PSEdition -eq 'Core') { return $Json | ConvertFrom-Json -AsHashtable } Add-Type -AssemblyName System.Web.Extensions $Serializer = New-Object System.Web.Script.Serialization.JavaScriptSerializer $Serializer.MaxJsonLength = [int]::MaxValue return $Serializer.DeserializeObject($Json) } |