2

I have a JSON object which I get from a server. The key which I get the value from looks something like this:

var myJson = data.body.region.store.customer.name;

I am trying to get the name key here, but sometimes the JSON (which comes from a service I have no control over) will have some empty fields, like for instance name might not be defined so the object will actually look like this: data.body.region.store.customer. Sometimes too customer, or store, or region might not be defined (If the data doesn't exist the service doesn't return a null or empty string for the value).

So if I need the name what I am doing is this:

if(data.body.region.store.customer.name){
   //Do something with the name
}

But say even store isn't defined, it will not get the value for name(which I would expect to be undefined since it doesn't exist) and the program crashes. So what I am doing now is checking every part of the JSON before I get the value with AND operands:

if(data && data.body && data.body.region && data.body.region.store && data.body.region.store.customer && data.body.region.store.customer.name){
     //Do something with the name value then
}

This works, because it checks sequentially, so it first checks does data exist and if it does it checks next if data.body exists and so on. This is a lot of conditions to check every time, especially since I use the service a lot for many other things and they need their own conditions too. So to just check if the name exists I need to execute 6 conditions which of course doesn't seem very good performance wise (and overall coding wise). I was wondering if there is a simpler way to do this?

3
  • 1
    Possible duplicate of Accessing nested JavaScript objects with string key Commented Jan 15, 2016 at 19:53
  • 1
    I know that this is not strictly a dupe, but the solution to the oft-asked question I linked would fit your needs perfectly. Commented Jan 15, 2016 at 19:54
  • 1
    If lodash is okay you could use _.get() var value = _.get(data, "body.region.store.customer.name", undefined /*defaultValue*/) Commented Jan 15, 2016 at 19:55

2 Answers 2

4
var myJson = null;
try {
    myJson = data.body.region.store.customer.name;
}
catch(err) {
    //display error message
}
Sign up to request clarification or add additional context in comments.

Comments

1

You can try following

function test(obj, prop) {
    var parts = prop.split('.');
    for(var i = 0, l = parts.length; i < l; i++) {
        var part = parts[i];
        if(obj !== null && typeof obj === "object" && part in obj) {
            obj = obj[part];
        }
        else {
            return false;
        }
    }
    return true;
}

test(myJson, 'data.body.region.store.customer.name');

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.