0

I have a issue when I try to use DataTables JQuery plugin.

SyntaxError: JSON.parse: unterminated string at line 1 column 1572865 of the JSON data

I think it's the way that I encode my JSON from a large database cut in 3000 rows query, because if I do it in one time I get an out of memory error:

$rowNum = $this->db->query("SELECT ID WHERE CONDITION")->result_array();
$pas = 3000;
for($i = $rowNum[0]; $i < count($rowNum); $i = $i+$pas) { // Loop wrinting rows 3000 per 3000 in a JSON
    $begin = $rowNum[$i];
    $end = (empty($rowNum[$i+($pas-1)])) ? end($rowNum) : $rowNum[$i+($pas-1)];
    $query = "
            LARGE QUERY FOR HAVING THE WHOLE TABLE AND ID BETWEEN {$begin} AND {$end} ";
    $rows = $this->db->query($query)->result_array();
    $line = json_encode($rows);
    file_put_contents(FCPATH."application/ajax/GuyaforTable_brut.json", $line, FILE_APPEND | LOCK_EX);
}
$charMappings = [
    '][' => ',',
];
$content = file_get_contents(FCPATH."application/ajax/GuyaforTable_brut.json");
file_put_contents(FCPATH."application/ajax/GuyaforTable_brut.json", strtr($content, $charMappings));

Line 1 column 1572865 of the JSON :

... 
,{"NomForet":"Paracou","n_parcelle":"17","n_carre":"2","n_arbre":"1081.0","Surface":"6.25","i_arbre":"167370","X":"232.0","Y":"156.0",
"Xutm":"272952.1 -> 6 <- ",
"Yutm":"586592.38","UTMZone":"22","Lat":"5.3035345","Lon":"-53.048702","n_essence":"751","nomPilote":"mesupu","Densite":"1.1000000000000001","circonf":"60.0","code_vivant":"1","code_mesure":"0","campagne":"1995","circ_corr":"60.0","code_corr":"0","Famille":"Melastomataceae","Genre":"Miconia","Espece":"tschudyoides","Commerciale":"0","SourceBota":"Vern","indSurete":"-1.0"},{"NomForet"
...
6
  • For what purpose do you make such json? Maybe csv file will be more suitable? Commented Nov 2, 2017 at 14:14
  • For an ajax pagination with datatables datatables.net/manual/ajax, this is a large database and I need to use the datatables plugin. Commented Nov 2, 2017 at 14:22
  • The out of memory error is PHP hitting the max allowed memory allocation. Editing the server's php.ini more memory can be allowed. However, there is performance implications to take into consideration. IMO, multiple smaller queries would be better. As for the unterminated string error, the JSON is not properly formatted. If you can get the return data run it through json-lint.com or other synbtax checkers. My guess is an unescaped quote or similar special character. Commented Nov 2, 2017 at 14:46
  • Yes, I tried to change the max allowed memory allocation, but I think the database is too large, I reached the out of memory error when apache was using 1,7go I think it is the max allowed by the OS, because I changed apache max RAM allowed too. Thank you for jsonlint.com I searched a tool like this for this issue. Commented Nov 2, 2017 at 15:02
  • "Valid JSON" for jsonlint.com Commented Nov 2, 2017 at 15:40

1 Answer 1

1

Not sure if this is still an issue for you, but if you were getting that error inside Firefox devtools it seems that will be fixed in the upcoming version (61): https://bugzilla.mozilla.org/show_bug.cgi?id=1223726

The devtools was truncating responses over a certain size (not affecting the actual code).

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

1 Comment

This is very useful. I tested the Ajax call and the devtool truncates the response. After I sent the request via PostMan I was able to see the entire response.

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.