Created
August 17, 2021 01:00
-
-
Save akutaktau/904c18ba54ad2b30796c31c03da34fc4 to your computer and use it in GitHub Desktop.
Sample how to convert csv from KKM repo to json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
//path to local json file. | |
$json_uri = "C:\Dev\covid-19\covid-19-states-daily-cases.json"; | |
//path to KKM csv | |
$fetch_uri = "https://raw.githubusercontent.com/MoH-Malaysia/covid19-public/main/epidemic/cases_state.csv"; | |
//open csv | |
$file = fopen($fetch_uri,'r'); | |
//open json file | |
$handle = file_get_contents($json_uri); | |
//convert json to array | |
$arr = json_decode($handle,1); | |
//get current array size | |
$startSize = count($arr); | |
//set array template so the structure will follow current stucture | |
$template = [ | |
"date"=> "", | |
"perlis"=> 0, | |
"kedah"=> 0, | |
"pulau-pinang"=> 0, | |
"perak"=> 0, | |
"selangor"=> 0, | |
"negeri-sembilan"=> 0, | |
"melaka"=> 0, | |
"johor"=> 0, | |
"pahang"=> 0, | |
"terengganu"=> 0, | |
"kelantan"=> 0, | |
"sabah"=> 0, | |
"sarawak"=> 0, | |
"wp-kuala-lumpur"=> 0, | |
"wp-putrajaya"=> 0, | |
"wp-labuan"=> 0 | |
]; | |
//get last array | |
$last = end($arr); | |
//set date a day after last array | |
$curr_date = date('Y-m-d', strtotime($last['date'] . "+1 days")); | |
$counter = 0; | |
//read the csv | |
while (!feof($file)) { | |
//get content row | |
$line = fgetcsv($file); | |
//exclude header | |
if($line[0] != 'date') { | |
//if current date equal to csv date | |
if($curr_date == $line[0]) { | |
$template['date'] = $line[0]; | |
//lower case state name, remove . and replace space with - | |
$state = str_replace('.','',str_replace(' ','-',strtolower($line[1]))); | |
$template[$state] = (int)$line[2]; | |
$counter++; | |
//if completed 14 state | |
if($counter == 14) { | |
$counter = 0; | |
array_push($arr,$template); | |
//get next day from current record | |
$curr_date = date('Y-m-d', strtotime($curr_date . "+1 days")); | |
} | |
} | |
} | |
} | |
//get latest array size | |
$endSize = count($arr); | |
//compare if array has increase write to file. | |
if($endSize > $startSize) { | |
$handle = json_encode($arr,JSON_PRETTY_PRINT); | |
file_put_contents($json_uri,$handle); | |
} | |
fclose($file); | |
exit; | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
//path to KKM csv | |
$fetch_uri = "https://raw.githubusercontent.com/MoH-Malaysia/covid19-public/main/epidemic/cases_state.csv"; | |
//path to local json file. | |
$json_uri = "C:\Dev\covid-19\covid-19-states-daily-cases.json"; | |
//open csv | |
$file = fopen($fetch_uri,'r'); | |
//open json file and decode | |
$arr = json_decode(file_get_contents($json_uri),1); | |
//get last array | |
$last = end($arr); | |
$counter = 0; | |
//set array template so the structure will follow current stucture | |
$template = [ | |
"date"=> "", | |
"perlis"=> 0, | |
"kedah"=> 0, | |
"pulau-pinang"=> 0, | |
"perak"=> 0, | |
"selangor"=> 0, | |
"negeri-sembilan"=> 0, | |
"melaka"=> 0, | |
"johor"=> 0, | |
"pahang"=> 0, | |
"terengganu"=> 0, | |
"kelantan"=> 0, | |
"sabah"=> 0, | |
"sarawak"=> 0, | |
"wp-kuala-lumpur"=> 0, | |
"wp-putrajaya"=> 0, | |
"wp-labuan"=> 0 | |
]; | |
//set date a day after last array | |
$curr_date = date('Y-m-d', strtotime($last['date'] . "+1 days")); | |
//read the csv | |
while (!feof($file)) { | |
//get content row | |
$line = fgetcsv($file); | |
//exclude header | |
if($line[0] != 'date') { | |
//if current date equal to csv date | |
if($curr_date == $line[0]) { | |
$template['date'] = $line[0]; | |
//lower case state name, remove . and replace space with - | |
$state = str_replace('.','',str_replace(' ','-',strtolower($line[1]))); | |
$template[$state] = (int)$line[2]; | |
$counter++; | |
//if completed 14 state | |
if($counter == 14) { | |
$counter = 0; | |
//open json file with read and write access | |
$handle = @fopen($json_uri, 'r+'); | |
// create the file if needed | |
if ($handle === null) | |
{ | |
$handle = fopen($json_uri, 'w+'); | |
} | |
if ($handle) | |
{ | |
// seek to the end | |
fseek($handle, 0, SEEK_END); | |
// are we at the end of is the file empty | |
if (ftell($handle) > 0) | |
{ | |
$stat = fstat($handle); | |
ftruncate($handle, $stat['size']-1); | |
// move to end of file | |
fseek($handle, 0, SEEK_END); | |
// add the trailing comma | |
$comma = ",".PHP_EOL; | |
fwrite($handle, $comma, 1); | |
// add the new json string | |
fwrite($handle, json_encode($template,JSON_PRETTY_PRINT) . PHP_EOL."]"); | |
} | |
else | |
{ | |
// write the first event inside an array | |
fwrite($handle, json_encode(array($template),JSON_PRETTY_PRINT)); | |
} | |
// close the handle on the file | |
fclose($handle); | |
} | |
//get next day from current record | |
$curr_date = date('Y-m-d', strtotime($curr_date . "+1 days")); | |
} | |
} | |
} | |
} | |
fclose($file); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment