Delhi Assembly Results 2015 – JSON

To see authentic Delhi Assembly Election 2015 results, I visited Election Commission of India website. Being a web developer I was very disappointed to see how website is created. I think the NIC developers have taken an oath for not following any of web standards. It is full of inline CSS and JavaScript and endless nested tables for layout. In the current mobile-era, nobody bothered to make if mobile friendly.

For every constituency the page was getting refreshed. The only thing that impressed me, was page load time. The result pages are only HTML files, no external resource dependency. Then I thought that they could have used JSON and then showed result by filtering data. This gave me an idea to create a JSON result of all records and then play with it.


I noticed that the url of results are in following format:[constituency_code]

Luckily, It was easy to get code and name for all Delhi assembly constituencies as it was provided in Select dropdown to choose. Now, I only have to create a associated Array and fetch html for each url. Once I have the content, I can parse the html and extract the relevant data for each url and then join them together for comprehensive result data.

For HTML parsing I chose php-html-parser, an HTML DOM parser. It allows you to manipulate HTML. Find tags on an HTML page with selectors just like jQuery.

The PHP script is as follows:

Gist @GitHub



        $className = str_replace("_", "\\", $className);
        $className = ltrim($className, '\\');
        $fileName = '';
        $namespace = '';
        if ($lastNsPos = strripos($className, '\\'))
            $namespace = substr($className, 0, $lastNsPos);
            $className = substr($className, $lastNsPos + 1);
            $fileName = str_replace('\\', DIRECTORY_SEPARATOR, $namespace) . DIRECTORY_SEPARATOR;
        $fileName .= str_replace('_', DIRECTORY_SEPARATOR, $className) . '.php';

        require $fileName;

use PHPHtmlParser\Dom;

$Constituencies = array(
		"4"=>"ADARSH NAGAR",
		"20"=>"CHANDNI CHOWK",
		"38"=>"DELHI CANTT",
		"61"=>"GANDHI NAGAR",
		"28"=>"HARI NAGAR",
		"70"=>"KARAWAL NAGAR",
		"23"=>"KAROL BAGH",
		"60"=>"KRISHNA NAGAR",
		"58"=>"LAXMI NAGAR",
		"43"=>"MALVIYA NAGAR",
		"12"=>"MANGOL PURI",
		"21"=>"MATIA MAHAL",
		"18"=>"MODEL TOWN",
		"25"=>"MOTI NAGAR",
		"11"=>"NANGLOI JAT",
		"40"=>"NEW DELHI",
		"24"=>"PATEL NAGAR",
		"44"=>"R K PURAM",
		"64"=>"ROHTAS NAGAR",
		"19"=>"SADAR BAZAR",
		"49"=>"SANGAM VIHAR",
		"63"=>"SEEMA PURI",
		"15"=>"SHAKUR BASTI",
		"14"=>"SHALIMAR BAGH",
		"29"=>"TILAK NAGAR",
		"16"=>"TRI NAGAR",
		"32"=>"UTTAM NAGAR",
		"59"=>"VISHWAS NAGAR",

$result = array();

foreach ($Constituencies as $code => $constituency_name) {

	$constituency = array();

	$constituency['code'] = $code;
	$constituency['name'] = ucwords(strtolower($constituency_name));

	$dom = new Dom;


	$rows = $dom->find(' table table table table tr');

	$candidates = array();

	foreach ($rows as $row){

	    $tds = $row->find('td');

	    if(count($tds) === 3){
	    	$candidate = array();
	    	$candidate['candidate'] = ucwords(strtolower($tds[0]->innerHtml));
	    	$candidate['party'] = $tds[1]->innerHtml;
	    	$candidate['votes'] = $tds[2]->innerHtml;
	    	$candidates[] = $candidate;

	$constituency['result'] = $candidates;

	$result[] = $constituency;

header('content-type: application/json; charset=utf-8');
echo json_encode($result);

Now that I have the consolidated result, the next step is to play with the data and create some UI. I will update the post when I have something substantial to share.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.