0

I'm a real noob when it comes to JSON. Any help on the following would be fantastic.

console.log(obj.id); in the code below returns nothing in the console - I need to understand why? I expect it two log two things in the console based on the JSON data.

JS:

var matchTeamAStatsJSON

$.ajax({ 
  type: 'GET', 
  url: 'http://www.website.com/apipathblahblahblah',
  data: { get_param: 'value' }, 
  dataType: 'json',        
  success: function (data) {
    matchTeamAStatsJSON = data;
    console.log(matchTeamAStatsJSON);
    for(var i = 0; i < matchTeamAStatsJSON.length; i++) {
      var obj = matchTeamAStatsJSON[i];
      console.log(obj.id);
    }
  }
})

JSON:

{
"records": [
    {
        "id": "recGWUWqwjUNLpekA",
        "fields": {
            "playerSprints": 12,
            "playerDistanceCovered_km": 6.23
        },
        "createdTime": "2018-03-22T18:16:56.000Z"
    },
    {
        "id": "recx5pMFpxnRwR4La",
        "fields": {
            "playerSprints": 12,
            "playerDistanceCovered_km": 6.23
        },
        "createdTime": "2018-03-19T11:35:11.000Z"
    }
]
}
Kevin B
  • 94,570
  • 16
  • 163
  • 180
user1951962
  • 125
  • 1
  • 14
  • try debugging first by just trying to log `obj` like `console.log(obj)` and see what that returns – sadmicrowave Mar 23 '18 at 15:56
  • i mean... your json clearly represents an object. Why are you treating it like an array? – Kevin B Mar 23 '18 at 15:57
  • Possible duplicate of [Access / process (nested) objects, arrays or JSON](https://stackoverflow.com/questions/11922383/access-process-nested-objects-arrays-or-json) – Kevin B Mar 23 '18 at 15:58
  • If you built the API, just return the result without the "records" key and it will work...Also try "data.d" – Tez Wingfield Mar 23 '18 at 16:04
  • Thanks to all who helped - Yosvel Quintero's answer below I think is what I was looking for – user1951962 Mar 23 '18 at 16:27

2 Answers2

3

You could use Array.prototype.forEach() and do:

const data = {"records": [{"id": "recGWUWqwjUNLpekA","fields": {"playerSprints": 12,"playerDistanceCovered_km": 6.23},"createdTime": "2018-03-22T18:16:56.000Z"},{"id": "recx5pMFpxnRwR4La","fields": {"playerSprints": 12,"playerDistanceCovered_km": 6.23},"createdTime": "2018-03-19T11:35:11.000Z"}]};

data.records.forEach(obj => console.log(obj.id));
Yosvel Quintero
  • 18,669
  • 5
  • 37
  • 46
  • Thank you, this helped - one more quick question, if I want to do more than one commend for each record - is the best way jst to duplicate this line: data.records.forEach(obj => console.log(obj.id)); - and replace console.log each time? – user1951962 Mar 23 '18 at 16:29
  • Just for comment one that more property values: `console.log(obj.id, obj.createdTime)` – Yosvel Quintero Mar 23 '18 at 16:37
  • Also notice that with arrow function you can have multiple lines of code: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Arrow_functions – Yosvel Quintero Mar 23 '18 at 16:38
-1

If the JSON example you posted below is the response from the GET request, data is equal to "records" which doesn't have an and "id" property. However, each instance of the array it contains does.

You need to get inside that array first and then get the "id" property of each element: console.log(obj.records[i].id) should get you want.

Hope this helps!