1

I'm trying to figure out how to check if a deeply nested JSON object, with several unknown arrays and properties contains a property that I'm looking for. I'm looking for a property that is called "isInvalid". If the field is there and the value of that key is true. I want to return false.

var checkValidity = function (data) {
    for (var property in data) {
        if (data.hasOwnProperty(property)) {
            if (property == "isInvalid" && data[property] === true) {
                return false;
            }
            else {
                if (typeof data[property] === "object" && data[property] !== null) {
                    this.checkValidity(data[property]);
                }
            }
        }
    }
};

This is the code I've been trying out but I'm unable to get that to work. I have been looking into underscore also, but cant find the needed functions. Anyone has an idea? (No reg exp please)

petur
  • 1,366
  • 3
  • 21
  • 42
  • @peter you can use Object.keys then check if the isInvalid is existing in the array of keys – Jairo Malanay Feb 17 '16 at 08:06
  • 1
    `this.checkValidity` is invalid, `checkValidity` is a variable, not a property of an object unless you are attaching it to some object and not showing it – Patrick Evans Feb 17 '16 at 08:07
  • please use a named function for recursion. – Nina Scholz Feb 17 '16 at 08:09
  • Is `isInvalid` key of the input object or you want to apply logic over there to test valid object ? – Rayon Feb 17 '16 at 08:15
  • I just wan't to check if the object, and the nested properties actually contains that key, and check if the value is true. I don't wan't any more logic what so ever. – petur Feb 17 '16 at 08:17

4 Answers4

3

If you really just want to check for property presence regardless of its particular location within JSON, then the easiest/fastest way is substring search in the source JSON string. If the latter is well-formed, then the property should be encoded in JSON as '"isInvalid":true'.

var checkValidity = function (jsonstr) {
    return jsonstr.indexOf('"isInvalid":true') >= 0;
}
hindmost
  • 7,125
  • 3
  • 27
  • 39
1

You can check like this

var s = {a:'1',b:'2'};

if(Object.getOwnPropertyNames(s).indexOf('a') != -1){
console.log('available');
}else{
  console.log('Not available');
};

editing answer... UPDATE

var s = {
  a1: '1',
  b: '2',
  c: {
    a: '11'
  }
};
var checkValidity = function (data) {
  if (Object.getOwnPropertyNames(data).indexOf('a') != - 1) {
    console.log('Found that key!!!');
  } else {
    for (var property in data) {

      if (Object.getOwnPropertyNames(property).indexOf('a') != - 1) {
         console.log('Found that key!!!');

      } else {
        if (typeof data[property] === 'object' && data[property] !== null) {
          console.log('not found continue in inner obj..');
          this.checkValidity(data[property]);
        }
      }
    }
  };
};
checkValidity(s);
saravanakumar
  • 1,747
  • 4
  • 20
  • 38
1

It tests for every nesting level the property isInvalid and if not, all other properties as object and their content. Array#every breaks if one return is false.

function checkValidity(data) {
    return !data.isInvalid && Object.keys(data).every(function (property) {
        if (typeof data[property] === "object" && data[property] !== null) {
            return checkValidity(data[property]);
        }
        return true;
    });
}

var data = {
    a: 1,
    b: 2,
    c: {
        isInvalid: true,
        a: false
    }
};

document.write('checkValidity() should be false: ' + checkValidity(data) + '<br>');
data.c.isInvalid = false;
document.write('checkValidity() should be true: ' + checkValidity(data));
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392
0

For complex json searching like this, I would use jsonpath ( http://goessner.net/articles/JsonPath/ ) which is the JSON equivalent of xpath.

To find the isInvalid field no matter where it is in the json, you would use it like this:

 jsonPath(data, "$..isInvalid")
bwbrowning
  • 6,200
  • 7
  • 31
  • 36