Skip to content
Advertisement

Cannot echo or json_decode long JSON string

I’m working with a large JSON data file (25 mb) in PHP.

Right now, I’m able to get the file and check it’s string length, which I get 24479798 for, although I can’t echo out the string.

After the echo strlen(), the rest of the script breaks down and I get no further outputs, including the echo "Made it to the bottom";

Why can’t I get json_decode to work? Is this a script memory problem, a character encoding problem in the JSON data? I’m pretty stuck.

<?php

error_reporting(E_ALL);
ini_set('error_reporting', E_ALL);   

if($result = file_get_contents("test.json")){
    echo "GOT IT";
}else{
    echo "NO";
}

// This works

echo "Got to here";

// This works

echo strlen($result);

// I get "24479798", which is the file string length

var_dump($diffbotResult);

// Outputs all the JSON data, so I know I have it and it's proper JSON data

$result = json_decode($result, true);

echo json_last_error_msg();

// No output

print_r($result);

// No output

echo "Made it to the bottom";

// Does not echo out anything

?>

Advertisement

Answer

Considering your trying to import a large file, the script runs longer than a browser/server will like, and will timeout before it finishes. Scripts like this need to be run on the command line or via cron to eliminate the timeout. You can also increase the memory by adding ini_set('memory_limit', '512M'); (or higher, as needed) to the beginning of the script to make sure it has enough memory to load and process the json file.

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement