Parse allows users to download their data using their Export tool, but only allows the data to be exported in JSON format. I want this in CSV format for analysis in Excel.
While a simple script suffices for smaller JSON objects, I am dealing with a dataset that is 670,000 rows and over 360MB. Online converters cannot handle this file size, frequently citing that PHP has exceeded its memory limit.
I have tried PHP CLI-based scripts and online converters, but they all seem to exceed their allocated memory. I figured I needed a new approach when ini_set('memory_limit', '4096M'); still didn't give me enough memory.
I am currently using this CLI-based script for parsing data:
// flatten to CSV
function flatten2CSV($file){
$fileIO = fopen($file, 'w+');
foreach ($this->dataArray as $items) {
$flatData = array();
$fields = new RecursiveIteratorIterator(new RecursiveArrayIterator($items));
foreach($fields as $value) {
array_push($flatData, $value);
}
fputcsv($fileIO, $flatData, ";", '"');
}
fclose($fileIO);
}
// and $this->dataArray is created here
function readJSON($JSONdata){
$this->dataArray = json_decode($JSONdata,1);
$this->prependColumnNames();
return $this->dataArray;
}
private function prependColumnNames(){
foreach(array_keys($this->dataArray[0]) as $key){
$keys[0][$key] = $key;
}
$this->dataArray = array_merge($keys, $this->dataArray);
}
How can I solve memory management issues with PHP and parsing through this large dataset? Is there a better way to read in JSON objects than json_decode for large datasets?