Thursday, April 5, 2012

JSON - Encountered unexpected character

Thought of sharing a nasty issue which i faced while testing a WCF Ajax enabled REST service which i had developed. This issue crept in only after i deployed the service in our testing server. When i was testing a service method in Fiddler i started getting this strange error "Message=Encountered unexpected character". I started searching StackOverflow for the root cause of this issue and many of the solutions were pointing at usage of single quote instead of double quote etc. But in my case that was not the issue because i pretty much use double quotes when i structure a json.

So what could be the issue? I have a service description document created which defines all the service methods and the request and response in json format. All i am doing is copy-pasting the json content and testing the same. Wait! copy-pasting from a word document? Am i introducing some junk characters because of the styles used by MS Word? Maybe.

I copy-pasted the json content to notepad so that all the styles get removed. Bingo, i felt i had found the solution.  I called the service method again. Aarrrggghhhh...."Encountered unexpected character" error was still existing. My hopes started fading and all i could do was stare at the json in notepad sipping a cup of coffee.

Wait! Wait! What in the name of double quotes am i seeing now? The quotes at many places were looking different. Different formats of quote in the same document with the same font style? I started replacing the existing double quotes by typing in from notepad and all started looking consistent and upright. I used the modified one in Fiddler and called the service method again.

The most satisfying feeling at the end of the day! I got the success response :)

To hell with ms word and its double quotes!!!, the one stop shop for all your mobile application development requirements

No comments: