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

Pranay
Sep 02, 2014  ยท  1073 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
}
AUTHOR

Pranay

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


Post a comment




Sign up for our newsletter

Subscribe to receive updates on our latest posts.

Thank you! You are now subscribed.