Muffasa
Muffasa

Reputation: 218

Is there a simple way to tell if json object contains a property?

I want to know if a property exists in a json tree, no matter in what depth.

isPropertyExists(@JSON@,@property name@)// that returns [@is exists@,@property value@,@property path@]

example:

var jsonObj={
             lvl1a:{
                   lvl2a:{lvl3:"some value"},
                   lvl2b:{lvl3a:"some value",
                          lvl3b:"some value"}
                   },
             lvl1b:{
                   lvl2aa:"some value",
                   lvl2bb:{target1:"some value"}
                   }    
             }  
isPropertyExists(jsonObj,'lvl1a') // returns [true,jsonObj.lvl1a,'jsonObj.lvl1a']  
isPropertyExists(jsonObj,'target1') // returns [true,jsonObj.lvl1b.lvl2bb.target1,'jsonObj.lvl1b.lvl2bb.target1'] 
isPropertyExists(jsonObj,'target2') // returns false 

Upvotes: 1

Views: 158

Answers (3)

J. Kovacevic
J. Kovacevic

Reputation: 193

Solution without path:

function hasProp(obj, prop) {
    Object.keys(obj).forEach(function (key) {
        if (key === prop) {
            return [true, obj[key]];
        } else if (typeof obj[key] === 'object') {
            hasProp(obj[key], prop);
        } else {
            return false;
        }
    });
};

Upvotes: 2

MrAlex6204
MrAlex6204

Reputation: 167

Yo need to implement a recursively function this will call itself

 function isPropertyExists (Json,findPropertyName,rootName) {
            var found = false;
            rootName+='->';
            for(iPropertyName in Json){
                  var iPropertyObject = Json[iPropertyName];                  
                  rootName+=iPropertyName;

                  console.log(rootName);
                  if(iPropertyName == findPropertyName){
                        console.log('-Exist!');
                        found = true;
                        break;                        
                  } else{     
                          //===>If is an object is because could contains a nested Json
                          if(typeof iPropertyObject =='object' ){
                             //===>Call itself and if return true will exit from the for loop an return true
                            if(isPropertyExists(iPropertyObject,findPropertyName,rootName)){
                              found = true;
                              break;
                            }
                          }  
                  }
                  rootName+='->';

            }
      return found;
      }

      console.log('End Result:',isPropertyExists(jsonObj,'target1','Root'));

Upvotes: 0

szdc
szdc

Reputation: 71

This is done using simple recursion.

Use Object.keys (rather than Object.getOwnPropertyNames or a for..in loop because the latter two will iterate properties in the prototype chain).

The solution

Iterate over the enumerable properties of your object. If any of the property names match the name we're searching for, return true. Otherwise, if the property's value is another object, iterate over that one too.

An example method

function propertyExists(name, o) {
  var properties = Object.keys(o);
  for (var i = 0; i < properties.length; i++) {
    var property = properties[i];
    if (property === name)
      return true;
    if (typeof o[property] === 'object' && propertyExists(o[property], name))
        return true;
  }
  return false;
}

Upvotes: 1

Related Questions