0

I'm trying to figure out how to count different values in a VERY large JSON file.

Below I have included a very small portion of my 10,000+ lines JSON file. I need to count the total amount of "status":"booked" and "status":"free" in number form.

[
  {
    "eventId": 126363,
    "eventKey": "4749466",
    "objectLabelOrUuid": "uuid8083",
    "status": "booked",
    "quantity": 1,
    "ticketType": null,
    "holdTokenHash": null,
    "version": 3,
    "extraData": null
  },
  {
    "eventId": 126363,
    "eventKey": "4749466",
    "objectLabelOrUuid": "uuid11392",
    "status": "free",
    "quantity": 0,
    "ticketType": null,
    "holdTokenHash": null,
    "version": 6,
    "extraData": null
  },
  {
    "eventId": 126363,
    "eventKey": "4749466",
    "objectLabelOrUuid": "uuid8051",
    "status": "booked",
    "quantity": 1,
    "ticketType": null,
    "holdTokenHash": null,
    "version": 2,
    "extraData": null
  }
]

After converting them to number form i will be placing the values in an html document

Seats Booked: <a id="booked"></a> Seats Free: <a id="free"></a>

expected output:

Seats Booked: 2 Seats Free: 1

It is my understanding that i can use a foreach method to fetch the data but i am unable to count them. If you have ANY ideas or preferably documentation that you can link here just let me know here!

Akhil
  • 381
  • 1
  • 8
  • 22
Austin Leath
  • 85
  • 1
  • 1
  • 9

2 Answers2

1

You could count them while iterating.

For assigning the value to the elements, you could take all entries of the object and use the keys as id and assign the value to the elements.

Used techniques:

var data = [{ eventId: 126363, eventKey: "4749466", objectLabelOrUuid: "uuid8083", status: "booked", quantity: 1, ticketType: null, holdTokenHash: null, version: 3, extraData: null }, { eventId: 126363, eventKey: "4749466", objectLabelOrUuid: "uuid11392", status: "free", quantity: 0, ticketType: null, holdTokenHash: null, version: 6, extraData: null }, { eventId: 126363, eventKey: "4749466", objectLabelOrUuid: "uuid8051", status: "booked", quantity: 1, ticketType: null, holdTokenHash: null, version: 2, extraData: null }],
    count = { booked: 0, free: 0 };

window.onload = function() {
    data.forEach(({ status }) => count[status]++);
    Object.entries(count).forEach(([id, value]) =>
        document.getElementById(id).innerHTML = value);
};
Seats Booked: <a id="booked"></a> Seats Free: <a id="free"></a>
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392
0

Give the array a name

var Arr = [{ eventId: 126363, eventKey: "4749466", objectLabelOrUuid: "uuid8083", status: "booked", quantity: 1, ticketType: null, holdTokenHash: null, version: 3, extraData: null }, { eventId: 126363, eventKey: "4749466", objectLabelOrUuid: "uuid11392", status: "free", quantity: 0, ticketType: null, holdTokenHash: null, version: 6, extraData: null }, { eventId: 126363, eventKey: "4749466", objectLabelOrUuid: "uuid8051", status: "booked", quantity: 1, ticketType: null, holdTokenHash: null, version: 2, extraData: null }];
    

// and run a simple for loop to filter both values from each item

var Booked = 0,
  Free = 0;
for (var i = 0; i < Arr.length; i++) {
  if (Arr[i].status == "booked") {
    Booked++;
  } else {
    Free++;
  }
};

// now you can put these 2 variables into html like

document.getElementById("booked").innerHTML = Booked;
document.getElementById("free").innerHTML = Free;
Seats Booked: <a id="booked"></a> Seats Free: <a id="free"></a>
mplungjan
  • 169,008
  • 28
  • 173
  • 236
  • now, if i wanted to store my array in a file "data.json" could i import it like var Arr = $.getJSON('JsonData.json') ? – Austin Leath Jul 04 '18 at 09:34