I recently retrieved JSON data from the firebug console and I am trying to calculate the total number of occurrences for the item "Open" in order to group the results by Team. This data is being extracted from our SharePoint list.
"d":
{
"results":
[
{
"__metadata":
{
"id": "Web/Lists(guid'1234578785596655')/Items(53)",
"uri": "https://spteamsite.com/_api/web/lists/GetByTitle('Teams')/Items(53)",
"etag": ""18"",
"type": "SP.Data.TasksListItem"
},
"Team": "Team A",
"taskStatus": "Open"
},
{
"__metadata":
{
"id": "Web/Lists(guid'1234578785596655')/Items(54)",
"uri": "https://spteamsite.com/_api/web/lists/GetByTitle('Teams')/Items(54)",
"etag": ""97"",
"type": "SP.Data.TasksListItem"
},
"Team": "Team B",
"taskStatus": "Open"
},
{
"__metadata":
{
"id": "Web/Lists(guid'1234578785596655')/Items(82)",
"uri": "https://spteamsite.com/_api/web/lists/GetByTitle('Teams')/Items(82)",
"etag": ""65"",
"type": "SP.Data.TasksListItem"
},
"Team": "Team B",
"taskStatus": "Open"
},
{
"__metadata":
{
"id": "Web/Lists(guid'1234578785596655')/Items(97)",
"uri": "https://spteamsite.com/_api/web/lists/GetByTitle('Teams')/Items(97)",
"etag": ""18"",
"type": "SP.Data.TasksListItem"
},
"Team": "Team C",
"taskStatus": "Open"
},
{
"__metadata":
{
"id": "Web/Lists(guid'1234578785596655')/Items(99)",
"uri": "https://spteamsite.com/_api/web/lists/GetByTitle('Teams')/Items(99)",
"etag": ""8"",
"type": "SP.Data.TasksListItem"
},
"Team": "Team E",
"taskStatus": "Open"
},
{
"__metadata":
{
"id": "Web/Lists(guid'1234578785596655')/Items(106)",
"uri": "https://spteamsite.com/_api/web/lists/GetByTitle('Teams')/Items(106)",
"etag": ""44"",
"type": "SP.Data.TasksListItem"
},
"Team": "Team D",
"taskStatus": "Open"
},
After implementing my JavaScript code, I realized that I am not getting the correct count. The value returned is only 3, whereas it should be 300 or even more based on the data set. Here is a snippet of my JSON data for reference.
// JavaScript source code
$.ajax({
url: _spPageContextInfo.webServerRelativeUrl + "/_api/web/lists/GetByTitle('Teams')/Items?$filter=taskStatus eq 'Open'&$select=Team,taskStatus",
type: "GET",
dataType: "json",
async: "true",
headers: {
"accept": "application/json;odata=verbose",
},
success: function (data) {
var dataArray = [];
var countArray = [];
var results = data.d.results;
for (var i = 0; i < results.length; i++){
for (key in results[i]) {
if (results[i].hasOwnProperty(key)) {
countArray.push(results[i][key]);
}
}
}
for (var i = 0; i < results.length; i++){
var team = Object.keys(results[1]).length; //This is returning only the value of 3
console.log(team);
}
console.log(countArray);
},
error: function(err) {
alert(JSON.stringify(err));
}
});
If you can provide some guidance on how to resolve this issue, it would be greatly appreciated.
I have made adjustments to the original JSON data as requested to ensure accuracy.