Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Checking if an element exists in json

using the following feed:

http://api.twitter.com/1/statuses/user_timeline.json?screen_name=microsoft&include_rts=1&count=10

I am successfully able to loop through this to get the details I want to display on my html page.

However, I need to check if retweeted_status.user.profile_image_url exists, and I am not sure how to do that?

Currently I am looping through the data where data is returned by jquery-ajax:

data[i].retweeted_status.user.profile_image_url

if data[i].retweeted_status.user.profile_image_url does not exist, it does not return null or undefined, I just get the following error:

cannot read property 'user' of undefined

like image 528
oshirowanen Avatar asked Jul 01 '11 12:07

oshirowanen


People also ask

How to check if a key exists in a JSON object?

JavaScript | Check if a key exists inside a JSON object. Given a JSON Object, the task is to check whether a key exists in Object or not using JavaScript. We’re going to discuss few methods. hasOwnProperty() This method returns a boolean denoting whether the object has the defined property as its own property (as opposed to inheriting it).

How to check if percentage key is present or not in JSON?

And you wanted to check if the percentage key is present or not in JSON data. if it is present directly to access its value instead of iterating the entire JSON. Note: We used json.loads () method to convert JSON encoded data into a Python dictionary. After turning JSON data into a dictionary, we can check if a key exists or not.

How to check whether a key exists in object using JavaScript?

Given a JSON Object, the task is to check whether a key exists in Object or not using JavaScript. We’re going to discuss few methods. hasOwnProperty() This method returns a boolean denoting whether the object has the defined property as its own property (as opposed to inheriting it). Syntax: obj.hasOwnProperty(prop) Parameters:

How to check for a value in any level in JSON?

Below function can be used to check for a value in any level in a JSON function _isContains (json, value) { let contains = false; Object.keys (json).some (key => { contains = typeof json [key] === 'object' ? _isContains (json [key], value) : json [key] === value; return contains; }); return contains; }


1 Answers

I think it's a good answer:

if('key' in myObj)
like image 54
z_inx Avatar answered Oct 08 '22 09:10

z_inx