-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..

1 Answer 1

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);

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

1 Comment

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: stackoverflow.com/questions/4035742/…

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.