0

Hello Can you help me to filter such Json Object:

{
    "data": {
        "statuses": [{
            "entities": {
                "urls": [],
                "user_mentions": [{
                    "screen_name": "name1"
                }]
            },
            "entities": {
                "urls": [],
                "user_mentions": [{
                    "screen_name": "name2"
                }]
            },
            "entities": {
                "urls": [],
                "user_mentions": [{
                    "screen_name": "name3"
                }]
            }
        }]
    }
}

I need to get array with values of each key screen_name. For example: array = ["name1","name2","name3"] How i can do it without frameworks with only JS?

Thanks for your help. I have updated Json to such like this:

var obj = {
"data": {
    "statuses": [{
        "urls": [],
        "user_mentions": [{
            "indices": [
                3,
                16
            ],
            "id_str": "626444770",
            "id": 626444770,
            "name": "katheryn",
            "screen_name": "sella_sandra"
        }, {
            "indices": [
                "***",
                "***"
            ],
            "id_str": "21447363",
            "id": 21447363,
            "name": "KATY PERRY",
            "screen_name": "katyperry"
        }, {
            "indices": [
                "***",
                "***"
            ],
            "id_str": "21447363",
            "id": 21447363,
            "name": "KATY PERRY",
            "screen_name": "floyd"
        }]
    }]
}

I'm wanna to get array of screen_names.I wrote such code.But when i use for.It doesn't work here:

console.log (statuses[0].user_mentions[i].screen_name) I't silly mistake but I cannot find out how correct that. Help guys!

var statuses = obj.data.statuses;
for (var i=0; i<statuses.length; i++ )

 { if ( typeof statuses[0].user_mentions !== "undefined")
            {    
               for (var i=0; i<statuses.length; i++){
                  console.log (statuses[0].user_mentions[i].screen_name);
            }
        } 

    else console.log ="No senders";
}
5
  • 1
    That's not valid JSON. Commented Jan 20, 2016 at 18:43
  • Thanks for your help and edit @MikeC Commented Jan 20, 2016 at 18:50
  • statuses still makes no sense. "statuses":[{"entities": {...}, "entities": {...}, "entities": {...}}] Commented Jan 20, 2016 at 19:07
  • Possible duplicate of How to Extract values from nested json object and concatenate them? Commented Jan 20, 2016 at 19:23
  • It was realy not clever.Error was in 'statuses.length'.Thanks everyone for help Commented Jan 26, 2016 at 14:59

2 Answers 2

2

Your syntax is incorrect, since inside statuses array you cannot have another key value data.

Removing the entities key name assuming that it is simply an array of objects then try this

var obj = { "data": {
  "statuses": [
    {
      "urls": [],
      "user_mentions": [{
        "screen_name": "name1"
      }]
    },
    {  
      "urls": [],
      "user_mentions": [{
        "screen_name": "name2"
      }]
    },
    {  
      "urls": [],
      "user_mentions": [{
        "screen_name": "name3"
      }]
    }
  ]
} };

var statuses = obj.data.statuses;
var names = [];
for (var counter = 0; counter < statuses.length; counter++ )
{
   names.push( statuses[ counter ].user_mentions[0].screen_name ); 
}
console.log( names );
Sign up to request clarification or add additional context in comments.

6 Comments

That's not valid syntax.
Array "statuses" has a lot of keys not only "entities".I wrote such way to show what i need but array can consist much more keys.
@frostrock array cannot have keys, array can only have items. Please check your array by running it on console and correct the same.
@gurvinder372 You can't start an object like you did. Run your code in the console and you'll see a syntax error. Also, you can validate JSON easily using this tool.
How i can request user_mentions[0]? Because when I use console.log(statuses.user_mentions[0]).I get response :Cannot read property '0' of undefined
|
0

Given your updated data object, this returns an array of screen_names (with any duplicates removed. If you want to keep duplicates, declare names as an array and push to it instead.) I used a relatively verbose coding style to make it easier to see what's going on.

var obj = {
    "data": {
        "statuses": [{
            "urls": [],
            "user_mentions": [{
                "indices": [3,16],
                "id_str": "626444770",
                "id": 626444770,
                "name": "katheryn",
                "screen_name": "sella_sandra"
            }, {
                "indices": ["***","***"],
                "id_str": "21447363",
                "id": 21447363,
                "name": "KATY PERRY",
                "screen_name": "katyperry"
            }, {
                "indices": ["***","***"],
                "id_str": "21447363",
                "id": 21447363,
                "name": "KATY PERRY",
                "screen_name": "floyd"
            }]
        }]
    }
};

var names = {};
for (var i=0; i<obj.data.statuses.length; i++) {
  if (obj.data.statuses[i]["user_mentions"]) {
    var mentions = obj.data.statuses[i]["user_mentions"];
    for (var j=0; j<mentions.length;j++) {
      if (mentions[j]["screen_name"]) {
        names[mentions[j]["screen_name"]] = 1;
      }
    }
  }
}

var arrayOfScreenNames = Object.keys(names);
console.log(arrayOfScreenNames);

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.