0

I've tried the suggestions from the top answer of the following topic but I'm not finding much luck: Checking if a key exists in a JavaScript object?

Say I'm dealing with a large, nested javascript object such as the response from a call to the following API: https://www.whoisxmlapi.com/order_paypal.php?domainName=google.com&outputFormat=json

The response if you search for a domain which exists contains WhoisRecord.administrativeContact.name, and I can spit this out in my javascript no problem.

However, if you search for a domain which does not exist this key is not returned in the object (fair enough).

All I want to do right now is assign the value of WhoisRecord.administrativeContact.name to a variable - I am happy for this variable to be blank or undefined if this key does not exist within the object, however the problem is that when I try and assign WhoisRecord.administrativeContact.name to a variable or check for its existence I get the good old "cannot read property of undefined" error if the API is not returning this information.

I understand 100% why this is happening, I just don't know how to get round it. The thread I linked at the top suggests using the "in" operator, which I've tried, but I can't get it to ever recognise that the object does contain this key, it's just always returning false.

Any advice/prodding in the right general direction appreciated.

Community
  • 1
  • 1
MyNotes
  • 426
  • 4
  • 11
  • 3
    `var name = WhoisRecord && WhoisRecord.administrativeContact && WhoisRecord.administrativeContact.name` – CD.. Jun 01 '16 at 16:04
  • such a simple solution - thank you so much. i have been way overthinking this. – MyNotes Jun 01 '16 at 16:07
  • Please use the search before you ask a new question. Also, this has nothing to do with JSON. – Felix Kling Jun 01 '16 at 17:09
  • @FelixKling IMHO not a duplicate since the other question asks for *"a better way"* than doing `a && a.b && a.b.c` - which is pretty useless for OP. The accepted answer here by @CD is what OP is after. – le_m Jun 01 '16 at 17:55
  • @le_m: I don't see why it's useless. It shows the straightforward way (in the question) and other versions in the answers. – Felix Kling Jun 01 '16 at 17:57
  • @FelixKling I might be nitpicking, but OP asks for safely reading a nested property which is answered here. If OP were to only read the other question / answers, he would be told that there is a 'better' way which isn't true IMHO. – le_m Jun 01 '16 at 18:07

2 Answers2

3

Use logical AND like:

var name = WhoisRecord && WhoisRecord.administrativeContact && WhoisRecord.administrativeContact.name;
CD..
  • 72,281
  • 25
  • 154
  • 163
1

For most "one-off" use cases I agree with @CD's answer.

But sometimes, it is more useful to merge your 'dynamic' datastructure with default values to guarantee the existence of important properties. This might allow you to write simpler, cleaner code:

// Your request result could be empty:
var data = {};

// Assign default values:
data = Object.assign({}, {
  WhoisRecord: {
    createdDate: 'Never',
    administrativeContact: {
      name: 'None'
    }
  }
}, data);

// Relevant properties are now guaranteed to exist:
console.log(data.WhoisRecord.administrativeContact.name);
console.log(data.WhoisRecord.createdDate);

See e.g. jQuery.extend() for more interesting merging strategies.

le_m
  • 19,302
  • 9
  • 64
  • 74