Extracting entities using javascript


#1

Hi guys,

I am using JSON.parse() in java script to parse the json stream returned by the Rest api.
Here is the code that i have written:
var tweets = JSON.parse(httpResponse.text);
var Tweet = Parse.Object.extend(“TweetDB”);
var count = tweets.length;
if(count > 0)
{
for(var i = 0; i< count; i = i +1)
{
var tweet = new Tweet();
tweet.set(“text”, tweets[i].text);
tweet.set(“id_str”, tweets[i].id_str);
tweet.set(“created_at”, tweets[i].created_at);
tweet.set(“urls”, tweets[i].entities.urls[0].url);
.
.
.
Now for the first tweet, i am able to extract the url successfully. But from the second tweet, i am unable to extract the url from the tweet and it throws an error.

Also, each tweet in the JSON stream had a URL and if I comment the code where i extract the url, it works fine.

Can somebody please throw some light on it.

Thanks in advance.
Warm Regards,
Puneet


#2

Never mind. Solved the problem. Can be closed. :slight_smile:


#3

Glad to hear you resolved the issue, would you care to share the result with the rest of the community? :wink:


#4

Sure Andy. :smile:

The problem was that i didn’t check if there was a URL present in a tweet or not. If there is no URL present, the array will be empty and the code will throw an error. So for the first of all the tweets fetched, there was a URL present, but not for the second one.

I guess i am still a noob in programming. :pensive: