Check if json object is undefined

Question:

I am using the amazon product api, my requests return xml in which encode to json.

Some items in my feed to not have a price, therefore I get the following error

However I can retrieve a sale price. So I want to basically see if

this.ItemAttributes.ListPrice is undefined if so then look for this…

this.Offers.OfferListing.Price.FormattedPrice

How can I do this in jQuery?

I tried this..

Answer:

Nearly. You want to be checking for undefined, not null.

One way to do it may be :

If you want to cover for all falsy values (null, undefined, zero…) you may just turn the first line into :

More here about falsy values

You may write it more succintly using || or ? operators, but be sure to keep it readable.

Leave a Reply