How can i check if a child attribute of a json object is null? While firefox recognizes it, Chrome and IE 8 don't.
I have a json object like this:
centralData.phone.id;
centralData.address.id;
centralData.product.id;
//and many others
And i'd like to check if some of it's attributes might be null. I'm doing this and it works:
if(centralData.phone != null){
//Do things
}
But this don't, since it's not always i have a StockGroup
if(centralData.product.StockGroup != null){
//Error
}
So, how can i check if centralData.product.StockGroup
is null?
How can i check if a child attribute of a json object is null? While firefox recognizes it, Chrome and IE 8 don't.
I have a json object like this:
centralData.phone.id;
centralData.address.id;
centralData.product.id;
//and many others
And i'd like to check if some of it's attributes might be null. I'm doing this and it works:
if(centralData.phone != null){
//Do things
}
But this don't, since it's not always i have a StockGroup
if(centralData.product.StockGroup != null){
//Error
}
So, how can i check if centralData.product.StockGroup
is null?
- What are you trying to check exactly? – Sergio Tulentsev Commented Apr 18, 2012 at 15:12
- Nitpicking: You don't have a "JSON object". You have an object or you have JSON. The latter is a data format and nothing but a STRING. – Mörre Commented Apr 18, 2012 at 15:13
- I just edited to make more clear. Thanks for pointing that. – AdrianoRR Commented Apr 18, 2012 at 15:13
- @Mörre I didn't know it wasn't called json object. Guess i mistook it because of java's class JSONObject. – AdrianoRR Commented Apr 18, 2012 at 15:15
- For your 2nd example, if centralData.product does not exist, OF COURSE you get an error. You have to check like this: "if(centralData.product && centralData.product.StockGroup != null)" It IS a little inconvenient, but you have to check ALL ELEMENTS of the path individually and not just the leaf element of the tree structure. Not to mention that you should use !== (or ===) and be clear if you want to check null or undefined. – Mörre Commented Apr 18, 2012 at 15:15
4 Answers
Reset to default 4I suppose you would check if a property is undefined
instead
if(typeof (centralData.product || {}).StockGroup) !== "undefined") {
/* do something */
}
this kind of check was described on ajaxian site and the overall code is much shorter
You don't want to check if it's null
, you want to check if the property exists (it would be undefined
in this case). Your check works because you are using ==
instead of ===
, which converts between types (undefined == null
, but undefined !== null
).
When you want to check for a nested property, you need to check every level. I would remend to use the in
operator, as it checks if the property exists and ignores it's value.
This does what you want to do:
if("product" in centralData && "StockGroup" in centralData.product){
…
}
Instead of using null, try paring your object with undefined.
Since JavaScript employs lazy evaluation, you can perform checks like this with no detriment to performance:
if(centralData.product != null && centralData.product.StockGroup != null){
}