ConvertFrom-JSON doesn't allow you to modify the maxJSONLength value - by Mark E. Schill

Status : 


Sign in
to vote
ID 801353 Comments
Status Active Workarounds
Type Bug Repros 10
Opened 9/18/2013 7:58:55 AM
Access Restriction Public


ConvertFrom-JSON doesn't allow you to modify the maxJSONLength value. By default it is set to 2MB so if you have a JSON string that is longer it will error out trying to convert to a PowerShell object. 
Sign in to post a comment.
Posted by June Blender on 9/15/2015 at 2:40 AM
If it is not changed, at least add this limit to the cmdlet help.
Posted by wwjimf on 6/6/2014 at 9:54 AM
I had a torrid time getting the workaround to work, I think because the returned objecct from the Deserialize method is a generic dictionary. Here is a snipped of the code I ended up writing to generate a hash table of keys and values returned by my input JSON array:

        ## ConvertFrom-Json fails for large returned strings >2MB
        #$List = GET -Url $uri | ConvertFrom-Json
        ## trying the workaround
        $Json = GET -Url $uri
        $jsser = New-Object System.Web.Script.Serialization.JavaScriptSerializer
        $jsser.MaxJsonLength = $jsser.MaxJsonLength * 10
        $jsser.RecursionLimit = 99
        ## The DeserializeObject method returns a generic dictionary
        $List = $jsser.DeserializeObject($Json)
        $col = @()
            Foreach($Item in $List.getEnumerator())
                ## Powershell doesn't handle generics all that well (or my understanding is flawed)
                ## so i need to construct a new hash table using
                ## the $item.Keys collection and
                ## the $item.TryGetValue method
                ## which, just for sh*ts n giggles, uses an out variable
                ## which, guess what, powershell does not handle very well
                ## so I have to use the [ref] type in the method signature
                $myhash = @{}
                Foreach($key in $Item.keys)
                    #Write-Host "Key Is: $Key"
                    $val = ""
                    $IsSuccess = $Item.TryGetValue($key, [ref]$val)
                    #Write-Host "Value is: $Val"
                $col += New-PrimoHpamHost $myhash
        $Exception = $_ | Select -ExpandProperty Exception    
        If($Exception.Message -match 'Not Found')
            Write-Verbose "HpamHost $Hostname not found"
            $col = $null
            Throw $_
    return $col
Posted by RJ3838 on 10/3/2013 at 4:58 AM
While the workaround does process larger files (e.g. >100Mb) it does not produce a blank line between each item from the original json (as ConvertFrom-Json does). This means it cannot be passed into ConvertTo-Csv.

Is there a way in which ConvertFrom-Json can have the value increased directly retaining its expected function.