jeffery_the_wind jeffery_the_wind - 2 years ago 88
SQL Question

How to test if a string is JSON or not?

I have a simple AJAX call, and the server will return either a JSON string with useful data or an error message string produced by the PHP function

. How can I test whether this data is a JSON string or the error message.

It would be nice to use a function called
just like you can use the function
to test if something is an Array.

This is what I want:

if (isJSON(data)){
//do some data stuff
//report the error

Answer Source

Use JSON.parse

function isJson(str) {
    try {
    } catch (e) {
        return false;
    return true;
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download