check if a string is in JSON compatible format using javascript or jquery

- Pranay on Sep 02 '14
- 1000 views

There is no direct way in javascript/jquery to check if a string is in JSON compatible format, but we can achieve by a small work around.

$.parseJSON(data) is a jQuery method which is used to parse JSON compatible string to JSON.

try {
    jsondata = $.parseJSON(data);
    //if you reach here the it is a json compatible format
}
catch (e) {
    // not JSON compatible format
}

About the author

Pranay Deegoju
Sitecore Certified Professional

A Software Engineer by profession, a part time blogger and an enthusiast programmer. You can find more about me here.



Leave your comments on this post here