Skip to content Skip to sidebar Skip to footer

How To Check If Javascript Object Is Json

I have a nested JSON object that I need to loop through, and the value of each key could be a String, JSON array or another JSON object. Depending on the type of object, I need to

Solution 1:

I'd check the constructor attribute.

e.g.

var stringConstructor = "test".constructor;
var arrayConstructor = [].constructor;
var objectConstructor = ({}).constructor;

function whatIsIt(object) {
    if (object === null) {
        return"null";
    }
    if (object === undefined) {
        return"undefined";
    }
    if (object.constructor === stringConstructor) {
        return"String";
    }
    if (object.constructor === arrayConstructor) {
        return"Array";
    }
    if (object.constructor === objectConstructor) {
        return"Object";
    }
    {
        return"don't know";
    }
}

var testSubjects = ["string", [1,2,3], {foo: "bar"}, 4];

for (var i=0, len = testSubjects.length; i < len; i++) {
    alert(whatIsIt(testSubjects[i]));
}

Edit: Added a null check and an undefined check.

Solution 2:

You can use Array.isArray to check for arrays. Then typeof obj == 'string', and typeof obj == 'object'.

var s = 'a string', a = [], o = {}, i = 5;
functiongetType(p) {
    if (Array.isArray(p)) return'array';
    elseif (typeof p == 'string') return'string';
    elseif (p != null && typeof p == 'object') return'object';
    elsereturn'other';
}
console.log("'s' is " + getType(s));
console.log("'a' is " + getType(a));
console.log("'o' is " + getType(o));
console.log("'i' is " + getType(i));

's' is string'a' is array 'o' is object'i' is other

Solution 3:

An JSON object is an object. To check whether a type is an object type, evaluate the constructor property.

functionisObject(obj)
{
    return obj !== undefined && obj !== null && obj.constructor == Object;
}

The same applies to all other types:

function isArray(obj)
{
    return obj !== undefined && obj !== null && obj.constructor == Array;
}

function isBoolean(obj)
{
    return obj !== undefined && obj !== null && obj.constructor == Boolean;
}

function isFunction(obj)
{
    return obj !== undefined && obj !== null && obj.constructor == Function;
}

function isNumber(obj)
{
    return obj !== undefined && obj !== null && obj.constructor == Number;
}

function isString(obj)
{
    return obj !== undefined && obj !== null && obj.constructor == String;
}

function isInstanced(obj)
{
    if(obj === undefined || obj === null) { returnfalse; }

    if(isArray(obj)) { returnfalse; }
    if(isBoolean(obj)) { returnfalse; }
    if(isFunction(obj)) { returnfalse; }
    if(isNumber(obj)) { returnfalse; }
    if(isObject(obj)) { returnfalse; }
    if(isString(obj)) { returnfalse; }

    returntrue;
}

Solution 4:

If you are trying to check the type of an object after you parse a JSON string, I suggest checking the constructor attribute:

obj.constructor == Array || obj.constructor == String || obj.constructor == Object

This will be a much faster check than typeof or instanceof.

If a JSON library does not return objects constructed with these functions, I would be very suspiciouse of it.

Solution 5:

You could make your own constructor for JSON parsing:

varJSONObj = function(obj) { $.extend(this, JSON.parse(obj)); }
var test = newJSONObj('{"a": "apple"}');
//{a: "apple"}

Then check instanceof to see if it needed parsing originally

test instanceofJSONObj

Post a Comment for "How To Check If Javascript Object Is Json"