I have a problem using PowerShell v3 when converting JSON strings over 2MB in size. The default limit in JSON serializer used by PowerShell is set to 2MB which explains the error.
However when I deserialize object using ConvertFrom-Json on a smaller set (I got various data objects with smaller and bigger internal collections but those are the same objects) it returns very nice object with all properties which I can easily access.
To overcome the limitations of the serializer I tried to deserialize data by hand:
$jsser = New-Object System.Web.Script.Serialization.JavaScriptSerializer
$jsser.MaxJsonLength = $jsser.MaxJsonLength * 10
$jsser.RecursionLimit = 99
$outObject = $jsser.DeserializeObject($json)
The object looks differently it seems that internal collections were not deserialized and when I try to execute properties they return empty results.
My questions:
Assumption is ConvertFrom-Json
does some additional magic or somehow creates a template for the object before serialization. Any idea how to replicate it?
The object I get is always a PSCustomObject
; if I get the object I want setup by ConvertFrom-Json
is there anyway to use it as object type in JsonSerializer?
The ConvertFrom-Json cmdlet converts a JavaScript Object Notation (JSON) formatted string to a custom PSCustomObject object that has a property for each field in the JSON string. JSON is commonly used by web sites to provide a textual representation of objects.
PowerShell is a great tool to use for manipulating JSON which is used throughout Azure. Have fun scripting! Additional reading: 7.1: ConvertFrom-Json (Microsoft.
I had the same problem and was able to solve it like this:
[void][System.Reflection.Assembly]::LoadWithPartialName("System.Web.Extensions")
$jsonserial= New-Object -TypeName System.Web.Script.Serialization.JavaScriptSerializer
$jsonserial.MaxJsonLength = 67108864
$Obj = $jsonserial.DeserializeObject($CourseTypesResponse)
You can use $jsonserial.MaxJsonLength
to manipulate the MaxJsonLength property
source: https://social.technet.microsoft.com/Forums/windowsserver/en-US/833c99c1-d8eb-400d-bf58-38f7265b4b0e/error-when-converting-from-json?forum=winserverpowershell&prof=required
I had the same propblem when using Invoke-RestMethod
and large JSON collections in the result. I ended up adapting the methods from Parsing json with PowerShell and Json.NET to convert JSON Collections to PowerShell Objects.
# .NET JSON Serializer
$global:javaScriptSerializer = New-Object System.Web.Script.Serialization.JavaScriptSerializer
$global:javaScriptSerializer.MaxJsonLength = [System.Int32]::MaxValue
$global:javaScriptSerializer.RecursionLimit = 99
# Functions necessary to parse JSON output from .NET serializer to PowerShell Objects
function ParseItem($jsonItem) {
if($jsonItem.PSObject.TypeNames -match "Array") {
return ParseJsonArray($jsonItem)
}
elseif($jsonItem.PSObject.TypeNames -match "Dictionary") {
return ParseJsonObject([HashTable]$jsonItem)
}
else {
return $jsonItem
}
}
function ParseJsonObject($jsonObj) {
$result = New-Object -TypeName PSCustomObject
foreach ($key in $jsonObj.Keys) {
$item = $jsonObj[$key]
if ($item) {
$parsedItem = ParseItem $item
} else {
$parsedItem = $null
}
$result | Add-Member -MemberType NoteProperty -Name $key -Value $parsedItem
}
return $result
}
function ParseJsonArray($jsonArray) {
$result = @()
$jsonArray | ForEach-Object {
$result += , (ParseItem $_)
}
return $result
}
function ParseJsonString($json) {
$config = $javaScriptSerializer.DeserializeObject($json)
return ParseJsonObject($config)
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With