function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
jbowesjbowes 

JSONObject and too many script statements, any suggestions?

I'm working on a project involving a callout to a service that returns a JSON result. Using JSONObject from code share I can parse the response as long as it's very small, but I am getting a Too Many Script Statements error on a lot of the responses. Making the response smaller from the other service isn't an option.

 

Anyone have any suggestions of ways I can handle parsing a larger JSON response?

Best Answer chosen by Admin (Salesforce Developers) 
jbowesjbowes

For those who have this problem and find this thread later, I tried making my own parser for the response, but the JSON is pretty deeply nested and it was taking a very long, painful, long, excruciating time. So I came up with another solution, which was to create a proxy service using the awesome Ruby project Goliath and running at Heroku that returns XML instead. I can then use the Apex XMLNode class and everything is nice.

 

Once I'm done with this project I'll post another thread with more details of the solution. Until salesforce adds native JSON parsing this is a pretty nice workaround that won't make you claw your own eyes out.

All Answers

WizradWizrad

That JSON Parser is very robust, but with governor limits, not practical.  You should write your own JSON parser specific to the JSON you are expecting back.

Rahul S.ax961Rahul S.ax961

JSON String which you generate, is just a string with proper syntax,

So instead make your own JSON string in apex,

I meant, proper Json string dynamically. :)

jbowesjbowes

For those who have this problem and find this thread later, I tried making my own parser for the response, but the JSON is pretty deeply nested and it was taking a very long, painful, long, excruciating time. So I came up with another solution, which was to create a proxy service using the awesome Ruby project Goliath and running at Heroku that returns XML instead. I can then use the Apex XMLNode class and everything is nice.

 

Once I'm done with this project I'll post another thread with more details of the solution. Until salesforce adds native JSON parsing this is a pretty nice workaround that won't make you claw your own eyes out.

This was selected as the best answer
geosowgeosow

I've already clawed my eyes out.  I'm looking forward to hearing your progress.  Can you type updates in braille?  :smileyhappy:

jbowesjbowes

Oh no! I don't do braile but maybe this will help.

 

What I ended up doing is using an asyc Ruby server project called Goliath. This link describes Goliath and shows an example of proxying the GitHub API, which was not too dissimliar from what I was looking to do:

 http://www.igvita.com/2011/03/08/goliath-non-blocking-ruby-19-web-server/

 

The newest Heroku release allows these apps to run there, here's an example of that: http://www.igvita.com/2011/06/02/0-60-deploying-goliath-on-heroku-cedar/

 

I built something similar but used Active Support and Yajl to take in the JSON from the service I wanted to read from and convert it to XML, then pass it through back to Apex. The result looks like this:

Apex callout to MyService -> MyService makes call to Other API -> MyServices parses JSON result from Other API -> MyService returns XML to Apex -> Dom.XMLNode action in Apex

 

I thought about just making a service that I could pass JSON to directly and get back XML, but I didn't need all of the response and dumping part of it was much easier and faster in Ruby than Apex so I chose the workflow above.  The only downside I've found is that using the free service level at Heroku, your app goes to sleep after about 20 minutes of inactivity, and when it's asleep the round trip takes 8-10 seconds longer while they spin the app back up. You can avoid that by paying for an extra dyno, which I might end up doing. For now, though, it's working like a charm.

 

I'm a little noobish with Ruby so I'm sure what I built could be better, but if you're stuck PM me and I can probably get you pointed in the right direction. Once the project is done and delivered I'll post a better description, but hopefully this helps in the meantime.