MyNotes MyNotes - 4 months ago 27x
JSON Question

How to check if a key exists anywhere within a nested javascript object?

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:

The response if you search for a domain which exists contains, 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 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 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.


Use logical AND like:

var name = WhoisRecord && WhoisRecord.administrativeContact &&;