JSON Parsing in Apex can be a Developer’s Nightmare!
Today I stumbled upon a requirement to parse JSON in Apex. I was happy about the fact that I know there is a cool open-source library called JSONObject, that will do all the heavy lifting of JSON parsing, and another cooler native JSON parser is lined up for the coming winter’12 release.
I was able to quickly set up the web service fixture using all the goodness of dependency injection for callouts in Apex.
JSON response vs JSONObject Parser: the bloody war!
The real pain started when I started parsing JSON responses. I met a series of awesome exceptions and errors, leaving me confused about what was wrong: JSON or Parser. I first got this exception about “Missing value”.
After doing some googling, I found a post where the code snippet indicated that newlines might be an issue with the parser. So I removed both “\r” & “\n” characters from the JSON response; why would the machine require a pretty printed JSON? Then life moved on for a while until I got this exception:
“FATAL_ERROR|superpat.JSONObject.JSONException: Expected a , or }”
I was again thinking, now what’s wrong here, JSON or Parser? Again, after some fighting with code, I ended up on Google searching for the same and luckily found Metadady already fixed this one. Many thanks to Metadady for fixing this issue and submitting a patch. I don’t know why this patch has not been applied to JSONObject since Jan 24, 2011.
Again, life moved a bit ahead, until the JSONObject started parsing some Unicode characters like “\u0026”, in the response. Again, Metadady was nice enough to mention this issue with his patch, but it seems there is no clean way to handle Unicode characters in Apex. So I decided to get rid of them, I can live without these special creatures in String
So, I kicked off all the Unicode awesomeness in JSON response with something like:
“jsonstring.replaceAll(‘\\\\u.{4}’, ”)”
Next, life moved really well, and my JSON was parsed successfully. I was having tears of joy. But those tears of joy turned into tears of sorrow when I saw the debug logs:
it took around 10+ seconds and
“154574” script lines for parsing a moderate JSON string.
Number of script statements: 154574 out of 200000 ******* CLOSE TO LIMIT
It was a strange feeling of losing everything after you won; words can’t express it.
I had some hope that I could get rid of it. But it was all lost when I read this post from legendary Jeff Douglas. It was an unlucky Googling day for me; if Jeff’s post had appeared earlier, I could have been happier by EOD.
Is it game over?
Anyway, the game is not yet over. I am waiting for the winter’12 release and native JSON parser, i.e. SYSTEM.JSON to come. Thanks to the Salesforce team for finally adding a native JSON parser to the Apex library.