Home  >  Q&A  >  body text

Unable to echo or parse long JSON strings

I'm processing a large JSON data file (25 mb) in PHP.

Now I can get the file and check its string length, and although I can't echo output the string, I get 24479798.

After echo strlen() the rest of the script crashes and I get no further output including echo "Made it to the Bottom";

Why can't I get json_decode to work? Is this a script memory issue, or a character encoding issue in the JSON data? I'm 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

?>

P粉116631591P粉116631591179 days ago364

reply all(1)I'll reply

  • P粉043295337

    P粉0432953372024-03-29 00:03:17

    Considering you are trying to import a large file, the script is taking longer to run than the browser/server needs and will time out before completing. Scripts like this need to be run from the command line or via cron to eliminate timeouts. You can also increase the memory by adding ini_set('memory_limit', '512M'); (or a higher value if needed) at the beginning of the script to ensure it has enough memory to load and process the json document.

    reply
    0
  • Cancelreply