I have a large csv dataset I need to transfer from a PHP server to JQuery using JSON. I am working on an embedded device with limited resources - so cannot read the whole file into memory. I have written a function to convert the csv file to a JSON file and then transfer the JSON file to the client, however I get a JSON syntax error on the client when requesting data using a JQuery AJAX call.
Convert CSV to JSON
public function convert_csv($csv, $json)
{
/*
* Open files.
*/
$csv_handle = fopen($csv, 'r');
$json_handle = fopen($json, "w");
/*
* Get the table headers.
*/
$headers = fgetcsv($csv_handle);
/*
* Write the array name.
*/
fwrite($json_handle, "\"LogData\":[");
$FirstRecord = true;
while ($row = fgetcsv($csv_handle))
{
/*
* Ensure no trailing comma after last record.
*/
if (!$FirstRecord)
fwrite($json_handle, ",");
else
$FirstRecord = false;
/*
* Create JSON record and write to file.
*/
$complete = array_combine($headers, $row);
fwrite($json_handle, json_encode($complete));
}
/*
* Close the array
*/
fwrite($json_handle, "]");
/*
* Close the files.
*/
fclose($csv_handle);
fclose($json_handle);
}
This is working:
CSV File
TimeStamp,Value
1390364805600.01,2.0
1390451205600.01,3.0
1390537605600.01,0.5
1390546245600.02,23.0
1390563525599.99,0.8
Creates JSON File
"LogData":[{"TimeStamp":"1390364805600.01","Value":"2.0"},{"TimeStamp":"1390451205600.01","Value":"3.0"},{"TimeStamp":"1390537605600.01","Value":"0.5"},{"TimeStamp":"1390546245600.02","Value":"23.0"},{"TimeStamp":"1390563525599.99","Value":"0.8"}]
PHP File Server
The following code is use to serve the JSON data from the server:
$this->logging_model->convert_csv($path, "/tmp/log.json");
//header('Content-Type: application/json'); // I have tried with and without this line
readfile("/tmp/log.json"); // push it out
I can confirm from within a browser that the JSON file as shown above is successfully transferred.
JQuery Client
I use the following the request the data from the client:
$.ajax({
url: URL,
type: "GET",
dataType: "json",
success: function(data) { SaveData(PointId, data);},
error: function (request, status, error) { alert(error);},
async: false
});
However I get a "SyntaxError: Invalid character" error when I attempt to download the data. I'm not sure where I have gone wrong - but any help would be appreciated.