June 19, 2010

Is my JSON Valid?




If you have been stuck with a JSON error and are not sure if your JSON is valid, use JSONLint.

JSONLint is a web-based tool for validating and reformatting JSON. Paste in some JSON and it’ll give you the syntax errors, if any. So let us say you give this tool the following input:

[{"ID":"1","Name":"Keith"}, {"ID":"2","Name":"Bill},
{"ID":"3","Name":"Govind"}, {"ID":"4","Name":"Kathy"}]

The tool returns an error message indicating unexpected TINVALID at line 9

Valid JSON Tool

What this error message means is that you have not enclosed your collection keys in quotes. On close observation, you can see the quotes on Bill (with ID 2) is not closed properly, hence causing the error. Specifying the quotes parses the JSON successfully. Also note that the tool neatly reformats the JSON output for us.

Valid JSON Tool

Pure JavaScript Version

There is a pure JavaScript version of the JSONLint service provided over here by Zach Carter, which makes it is easy to host this service on your own.

When I gave this tool the same input, the location of the error was much more easier to locate than I could do with the JSONLint service. Observe the arrow (circled) which pinpoints that the error is on the string Bill.

Valid JSON Tool

Bookmark this link for future use.



'Like' us on our FaceBook page if you find this blog useful. Thanks!


Did you like this post?
kick it on DotNetKicks.com Save on Delicious
subscribe via rss subscribe via e-mail
print this post follow me on twitter


About The Author

Suprotim Agarwal, ASP.NET Architecture MVP works as an Architect Consultant and provides consultancy on how to design and develop Web applications.

Suprotim is also the founder and primary contributor to DevCurry, DotNetCurry and SQLServerCurry. He has also written an EBook 51 Recipes using jQuery with ASP.NET Controls.

Follow him on twitter @suprotimagarwal

comments

1 Response to "Is my JSON Valid?"
  1. Larry Battle said...
    June 20, 2010 8:40 AM

    Thanks for the utilities.
    You can also use JSON.stringfy to reformat your object into the proper JSON format.
    Usage:
    JSON.stringify( obj )

 

Copyright © 2009-2012 All Rights Reserved for DevCurry.com by Suprotim Agarwal | Terms and Conditions