Home Dashboard Directory Help
Search

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


Status: 

Active


11
0
Sign in
to vote
Type: Bug
ID: 801353
Opened: 9/18/2013 7:58:55 AM
Access Restriction: Public
1
Workaround(s)
view
4
User(s) can reproduce this bug

Description

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.
Details
Sign in to post a comment.
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:

    Try
    {
        ## ConvertFrom-Json fails for large returned strings >2MB
        ## https://connect.microsoft.com/PowerShell/feedback/details/801353/convertfrom-json-doesnt-allow-you-to-modify-the-maxjsonlength-value
        #$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 = @()
        If($List)
        {
            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"
                    $myhash.Add($key,$val)
                }
                $col += New-PrimoHpamHost $myhash
            }
        }
    }
    Catch
    {
        $Exception = $_ | Select -ExpandProperty Exception    
        If($Exception.Message -match 'Not Found')
        {
            Write-Verbose "HpamHost $Hostname not found"
            $col = $null
        }
        Else
        {
            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.

Sign in to post a workaround.
Posted by Cody Konior on 9/28/2013 at 9:36 PM
$json = get-content "Y:\Desktop\bookmarks.json"

# $json | convertfrom-json # fails

$jsser = New-Object System.Web.Script.Serialization.JavaScriptSerializer
$jsser.MaxJsonLength = $jsser.MaxJsonLength * 10
$jsser.RecursionLimit = 99    

$outObject = $jsser.DeserializeObject($json)