-1

just need to get a specific count of a json data

here's the sample json format:

{
  "tickets": [
    {
      "url": "https://asd.zendesk.com/api/v2/tickets/1.json",
      "id": 1,
      "external_id": null,
      "via": {
        "channel": "sample_ticket",
        "source": {
          "from": {},
          "to": {},
          "rel": null
        }
      },
      "created_at": "2017-04-25T05:56:36Z",
      "updated_at": "2017-04-25T05:56:36Z",
      "status": "open",
    },
    {
      "url": "https://asd.zendesk.com/api/v2/tickets/2.json",
      "id": 2,
      "external_id": null,
      "via": {
        "channel": "sample_ticket",
        "source": {
          "from": {},
          "to": {},
          "rel": null
        }
      },
      "created_at": "2017-04-25T05:56:36Z",
      "updated_at": "2017-04-25T05:56:36Z",
      "status": "close",
    },
    {
      "url": "https://asd.zendesk.com/api/v2/tickets/2.json",
      "id": 2,
      "external_id": null,
      "via": {
        "channel": "sample_ticket",
        "source": {
          "from": {},
          "to": {},
          "rel": null
        }
      },
      "created_at": "2017-04-25T05:56:36Z",
      "updated_at": "2017-04-25T05:56:36Z",
      "status": "solve",
    }
    ]
}

i want to count how many open, close and solve status using php or ajax or both... need the data auto load also... so we don't need to refresh..

ivor
  • 307
  • 1
  • 6
  • 16

1 Answers1

2

Check this out:

var jsonObject = {
  "tickets": [
    {
      "url": "https://asd.zendesk.com/api/v2/tickets/1.json",
      "id": 1,
      "external_id": null,
      "via": {
        "channel": "sample_ticket",
        "source": {
          "from": {},
          "to": {},
          "rel": null
        }
      },
      "created_at": "2017-04-25T05:56:36Z",
      "updated_at": "2017-04-25T05:56:36Z",
      "status": "open",
    },
    {
      "url": "https://asd.zendesk.com/api/v2/tickets/1.json",
      "id": 1,
      "external_id": null,
      "via": {
        "channel": "sample_ticket",
        "source": {
          "from": {},
          "to": {},
          "rel": null
        }
      },
      "created_at": "2017-04-25T05:56:36Z",
      "updated_at": "2017-04-25T05:56:36Z",
      "status": "open",
    },
    {
      "url": "https://asd.zendesk.com/api/v2/tickets/2.json",
      "id": 2,
      "external_id": null,
      "via": {
        "channel": "sample_ticket",
        "source": {
          "from": {},
          "to": {},
          "rel": null
        }
      },
      "created_at": "2017-04-25T05:56:36Z",
      "updated_at": "2017-04-25T05:56:36Z",
      "status": "close",
    },
    {
      "url": "https://asd.zendesk.com/api/v2/tickets/2.json",
      "id": 2,
      "external_id": null,
      "via": {
        "channel": "sample_ticket",
        "source": {
          "from": {},
          "to": {},
          "rel": null
        }
      },
      "created_at": "2017-04-25T05:56:36Z",
      "updated_at": "2017-04-25T05:56:36Z",
      "status": "solve",
    }
    ]
};

var open = 0, close = 0; solve = 0;
for( var i = 0; i < jsonObject.tickets.length; i++)
{
  if(jsonObject.tickets[i].status == "open")
  {
   open++;
  }
  else if(jsonObject.tickets[i].status == "close")
  {
   close++;
  }
  else if(jsonObject.tickets[i].status == "solve")
  {
   solve++;
  }

}

console.log(open, close, solve);
KAD
  • 10,972
  • 4
  • 31
  • 73
  • You can use `json_decode`, the json will then be treated as a php array. Then you can apply the same concept to count the occurrence with values. Check this for reference: https://stackoverflow.com/questions/4035742/parsing-json-object-in-php-using-json-decode – KAD Jun 27 '17 at 14:09