0

I have an API and when I run the PowerShell script:

Invoke-WebRequest 'https://api.tfl.gov.uk/Place/Type/OysterTicketShop'|ConvertFrom-Json

I get the error:

ConvertFrom-Json : Error during serialization or deserialization using the JSON JavaScriptSerializer. The length of the string exceeds the value set on the maxJsonLength property. Parameter name: input At line:2 char:72 + Invoke-WebRequest 'https://api.tfl.gov.uk/Place/Type/OysterTicketShop'|ConvertFr ... + ~~~~~~~~~ + CategoryInfo : NotSpecified: (:) [ConvertFrom-Json], ArgumentException + FullyQualifiedErrorId : System.ArgumentException,Microsoft.PowerShell.Commands.ConvertFromJsonCommand

My understanding is the API is quite large but I haven't been able to resolve the issue.

I tried using:

Invoke-WebRequest 'https://api.tfl.gov.uk/Place/Type/OysterTicketShop'|ConvertFrom-Json 
[void][System.Reflection.Assembly]::LoadWithPartialName("System.Web.Extensions")
$jsonserial= New-Object -TypeName System.Web.Script.Serialization.JavaScriptSerializer 
$jsonserial.MaxJsonLength = $somevalue 
$Obj = $jsonserial.DeserializeObject($rawtext) 
$jsonserial.MaxJsonLength = [int]::MaxValue

I still got the same error.

1
  • 2
    I edited and formatted the code. Please check if I removed or added some whitespace that is harmful. Commented Oct 1, 2017 at 18:54

2 Answers 2

2

Welp, if you follow the logic above, you are trying to convert the webrequest object from JSON which it is not. If the body is what is in JSON then you need to do the following.

ConvertFrom-Json (Invoke-WebRequest 'https://api.tfl.gov.uk/Place/Type/OysterTicketShop').Content

That is about it.

Sign up to request clarification or add additional context in comments.

Comments

1

A couple of issues here.

Invoke-WebRequest 'https://api.tfl.gov.uk/Place/Type/OysterTicketShop'|ConvertFrom-Json 

You are still using the same method here. And then it seems like you have pasted some somewhat relevant code under it. What does $rawtext and $somevalue hold?

$jsonserial.MaxJsonLength = $somevalue 
$Obj = $jsonserial.DeserializeObject($rawtext) 
$jsonserial.MaxJsonLength = [int]::MaxValue

There is no need to change MaxJsonLength twice, especially not after the deserialization.

You want to remove the ConvertFrom-Json cmdlet because of the length limit and use the webrequest response for the JavaScriptSerializer.

$url = "https://api.tfl.gov.uk/Place/Type/OysterTicketShop"
[void][System.Reflection.Assembly]::LoadWithPartialName("System.Web.Extensions")
$jsonserial = New-Object -TypeName System.Web.Script.Serialization.JavaScriptSerializer
$jsonserial.MaxJsonLength = [int]::MaxValue
#Because of SSL requirements
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
#Getting webresponse/JSON data, stored in a varibale
$content = (Invoke-WebRequest $url).Content
#Deserializing the JSON data
$Obj = $jsonserial.DeserializeObject($content)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.