1

I have this data.json file and I need to get the values of the "hashtags" it is 10,000 json datas in my file so I include only the important one..

var data =
[
{
    "favorite_count": 0,
    "entities": {
        "hashtags": [
            {
                "text": "Hope",
                "indices": [
                    0,
                    5
                ]
            },
            {
                "text": "USA",
                "indices": [
                    6,
                    10
                ]
            },
            {
                "text": "Youth",
                "indices": [
                    11,
                    17
                ]
            },
            {
                "text": "sex",
                "indices": [
                    51,
                    55
                ]
            },
            {
                "text": "condoms",
                "indices": [
                    94,
                    102
                ]
            },
            {
                "text": "STD",
                "indices": [
                    120,
                    124
                ]
            },
            {
                "text": "HPV",
                "indices": [
                    135,
                    139
                ]
            }
        ]
    }
},
{
    "favorite_count": 0,
    "entities": {
        "hashtags": [
            {
                "text": "starbucks",
                "indices": [
                    3,
                    13
                ]
            }
        ]
    }
  }
]

So I have here a hashtags which I want to get ONLY the text and if it is a null it will not get anything.. I can't get the values and I don't know how to iterate it because I am not familiar in json.. here is my code by the way in javascript

$(document).ready(function()
{
   $("#hashtagBtn").click(function() 
   {
      $("#theTweets").html(graphHashtag());
   });
});



function graphHashtag()
{
   var getHashtags = [];

   $.each(data, function(i, obj)
   { 
      if(obj.hasOwnProperty("text") && data[i].lang == "en" && data[i].entities.hashtags != null)   
          getHashtags.push(obj.entities.hashtags[i].text);
   });

   return getHashtags;
}
1
  • I think you need to parse the JSON with JSON.parse(data). Commented Nov 24, 2014 at 1:59

1 Answer 1

1

you can use javascript for this kind of task... no jQuery wrapping is required

function graphHashtag()
{
   var tags= [];

   for (var i in data)
   {
       // data[i] is an object

       for (var j in data[i].entities.hashtags)
       {
           var text = data[i].entities.hashtags[j].text;

           if (text) tags.push(text);
       }
   }
   return tags;
}

i omitted some validations, but if every main object will have the entities and hashtags properties there shouldn't be any problem

Sign up to request clarification or add additional context in comments.

1 Comment

great! JSON is the shortage of JavaScript Object Notation.. so the name implies that it designed to be manipulated using JavaScript

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.