0

I have a JSON list that comes from a table in a database with multiple siblings.

"PlayerNumbers":{"PlayerNumber":[
{"Section":"1","Team":"1","Direction":"N","Number":"00280149","Name":" ANSART A","Updated":"False","TimeLog":"","Processed":"False"},
{"Section":"1","Team":"1","Direction":"S","Number":"00280131","Name":"ANSART Y","Updated":"False","TimeLog":"","Processed":"False"},
{"Section":"1","Team":"2","Direction":"N","Number":"01964990","Name":" LAHAYE B","Updated":"False","TimeLog":"","Processed":"False"}]}

Say I need to read the "Name" key where the Section=1 and Team=2 and Direction=N.

Edit: Of course I will work with the js parsed object

What would be the syntax in Javascript?

6
  • Firstly, the JSON you posed looks invalid. Secondly, JSON is just a notation. To use it in JavaScript, convert it to a native JavaScript Object and work with that Commented Mar 29, 2015 at 16:37
  • Are you really dealing with JSON (a string), or with the parsed result? And note that the given JSON is invalid, it needs { at the beginning and } at the end (or similar). Commented Mar 29, 2015 at 16:37
  • 2
    Where are you stuck? You simply look through the PlayerNumber array. Commented Mar 29, 2015 at 16:37
  • json.PlayerNumbers.PlayerNumber.map(function(a){return a.Section==1 && a.Direction=="N" && a.Name;}).filter(Boolean); Commented Mar 29, 2015 at 16:39
  • @dandavis probably also want to .filter before the .map Commented Mar 29, 2015 at 16:40

2 Answers 2

2

I'm going to assume you aren't dealing with JSON at all, but with an object tree parsed from JSON. And I'm going to assume what you've given is the content of an object definition, like this JSON:

{
    "PlayerNumbers": {
        "PlayerNumber": [
            {
                "Section": "1",
                "Team": "1",
                "Direction": "N",
                "Number": "00280149",
                "Name": " ANSART A",
                "Updated": "False",
                "TimeLog": "",
                "Processed": "False"
            },
            {
                "Section": "1",
                "Team": "1",
                "Direction": "S",
                "Number": "00280131",
                "Name": "ANSART Y",
                "Updated": "False",
                "TimeLog": "",
                "Processed": "False"
            },
            {
                "Section": "1",
                "Team": "2",
                "Direction": "N",
                "Number": "01964990",
                "Name": " LAHAYE B",
                "Updated": "False",
                "TimeLog": "",
                "Processed": "False"
            }
        ]
    }
}

So having parsed the JSON, you'll have an object with a PlayerNumbers property, which refers to an object with a PlayerNumber property, which refers to an array of objects with the information you want.

So you loop through the array until you find the first matching entry (assuming you only want the first one):

var name;
obj.PlayerNumbers.PlayerNumber.some(function(entry) {
    if (entry.Section == "1" && Team == "2" && entry.Direction == "N") {
        name = entry.Name;
        return true;
    }
    return false;
});

If you want all of the matches:

var names = obj.PlayerNumbers.PlayerNumber.filter(function(entry) {
    return (entry.Section == "1" && Team == "2" && entry.Direction == "N");
}).map(function(entry) { return entry.Name; });

Both of the above use ES5 features of Array missing on outdated engines. If you need to support outdated engines, all of the above can be "shimmed" or "polyfilled," a search should turn up the necessary shims/polyfills.

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

1 Comment

Thanks a lot for taking the time to summarize the question and giving such a precise answer, you've made my day :-)
1
var playersObject = { 
    "PlayerNumbers": {
    "PlayerNumber": [{
        "Section": "1",
        "Team": "1",
        "Direction": "N",
        "Number": "00280149",
        "Name": " ANSART A",
        "Updated": "False",
        "TimeLog": "",
        "Processed": "False"
    }, {
        "Section": "1",
        "Team": "1",
        "Direction": "S",
        "Number": "00280131",
        "Name": "ANSART Y",
        "Updated": "False",
        "TimeLog": "",
        "Processed": "False"
    }, {
        "Section": "1",
        "Team": "2",
        "Direction": "N",
        "Number": "01964990",
        "Name": " LAHAYE B",
        "Updated": "False",
        "TimeLog": "",
        "Processed": "False"
    }]
    }
};

   function getParticularPlayer(section, team, direction) {
     return playersObject.PlayerNumbers.PlayerNumber.filter(function(player) {
            return (player.Section===section && player.Team===team && player.Direction===direction); 
     });
   }

Now, you can use

   var results = getParticularPlayer("1","1","N");

It will return you array of matching players. If your sure that always only one player is returned, you can use results[0].

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.