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
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)
}
I put this in my code :
JavaScriptSerializer oSerializer = new JavaScriptSerializer();
oSerializer.MaxJsonLength *= 2;
ws_Out = (ClsWsOut)oSerializer.Deserialize(jsonOut, ws_Out.GetType());
Where ws_Out.GetType() is a classe I define to parse the json.
public class ClsLogin_In :ClsWsIn
{
public string login { get; set; }
public string passwd { get; set; }
}
public class ClsLogin_Out : ClsWsOut
{
public int error { get; set; }
public string error_desc { get; set; }
public int key { get; set; }
}
Edited
in PowerShell V3 when the json returned by a web service is very big the PowerShell V3 is sending an exception. So I use XML serialisation, here is my function, it also use an external assemblies, but they are basic ones, XML is a bit verbose but it works.
Add-Type -AssemblyName System.ServiceModel.Web, System.Runtime.Serialization
$utf8 = [System.Text.Encoding]::UTF8
function Write-String
{
PARAM([Parameter()]$stream,
[Parameter(ValueFromPipeline=$true)]$string)
PROCESS
{
$bytes = $utf8.GetBytes($string)
$stream.Write( $bytes, 0, $bytes.Length )
}
}
function Convert-JsonToXml
{
PARAM([Parameter(ValueFromPipeline=$true)][string[]]$json)
BEGIN
{
$mStream = New-Object System.IO.MemoryStream
}
PROCESS
{
$json | Write-String -stream $mStream
}
END
{
$mStream.Position = 0
try
{
$jsonReader = [System.Runtime.Serialization.Json.JsonReaderWriterFactory]::CreateJsonReader($mStream,[System.Xml.XmlDictionaryReaderQuotas]::Max)
$xml = New-Object Xml.XmlDocument
$xml.Load($jsonReader)
$xml
}
finally
{
$jsonReader.Close()
$mStream.Dispose()
}
}
}
function Convert-XmlToJson
{
PARAM([Parameter(ValueFromPipeline=$true)][Xml]$xml)
PROCESS
{
$mStream = New-Object System.IO.MemoryStream
$jsonWriter = [System.Runtime.Serialization.Json.JsonReaderWriterFactory]::CreateJsonWriter($mStream)
try
{
$xml.Save($jsonWriter)
$bytes = $mStream.ToArray()
[System.Text.Encoding]::UTF8.GetString($bytes,0,$bytes.Length)
}
finally
{
$jsonWriter.Close()
$mStream.Dispose()
}
}
}
Here is an example.
$json = @'
{
"data": {
"langid": 7,
"results": [{
"first_aired": "2010-11-15",
"name": "Accused",
"tvdbid": 72663
},
{
"first_aired": "2010-01-17",
"name": "Enzai: Falsely Accused",
"tvdbid": 135881
}]
},
"message": "",
"result": "success"
}
'@
$xmlOut = Convert-JsonToXml -json $json
($xmlOut.root.data.results).ChildNodes[0].tvdbid.InnerText
($xmlOut.root.data.results).ChildNodes[1].tvdbid.InnerText
This one worked for me quite well over the years:
Add-Type -Assembly System.Web.Extensions
function get-Json($data) {
$json = [System.Web.Script.Serialization.JavaScriptSerializer]::new()
$json.MaxJsonLength = 104857600
$out = $json.Deserialize($data, [System.Object])
return $out
}