0

so am trying to save the feed am getting from a url and writing it to a file called new.json. after writing the feed to the file, i then use the json_decode function to store the information but am unable to retrieve the information. i know the code works because i've tested it with another file and it works but the file is jus 6.45KB and the new.json file is over 3MB.

i need the data from the url to update a database, but am unable to because it jus wont produce the information from the file, so am wondering if the file is too large.

thanks.

$new_props = $property->getData($URL); //Set Method to retrieve  property
$file = fopen("new.json","w+")or die("Error opening output file");
echo fwrite($file,json_encode($new_props));
fclose($file);

$ourFileName = "new.json";
$mydata = file_get_contents($ourFileName);
$json_array = json_decode($mydata, TRUE);

if (is_null($json_array)) {
    echo'<br /> json decode failed. Error handling here. <br />';
}
$id = $json_array[property][1][id];
$area = $json_array[property][1][area];
echo "<br /> id:".$id ."<br /> area:".$area;
  • Do you have any error ? – Magicprog.fr Jul 13 '15 at 15:50
  • 1
    This depends on your PHP's `max_execution_time`, which can be overridden by `set_time_limit()`. – Praveen Kumar Purushothaman Jul 13 '15 at 15:51
  • 1
    What is the memory limit that PHP is allowed? There are memory limits but i would be amazed that a 3 MB JSON file was an issue. Have you checked the file to be valid with: [JSONLint -The JSON Validator](http://jsonlint.com/) – Ryan Vincent Jul 13 '15 at 15:53
  • Are you sure that the json in the 3MB is valid JSON ? The larger it is, the more prone to errors (stray single quotes etc). – ODelibalta Jul 13 '15 at 15:54
  • @PraveenKumar ive set the max_execution_time to 10000. – Steph-phpProgrammer Jul 13 '15 at 15:59
  • @RyanVincent memory_limit is 218M. ive tried check it on JSONLint but the code is so much that it freezes the site when i try pasting it in to text it. – Steph-phpProgrammer Jul 13 '15 at 16:00
  • @ODelibalta, my thoughts exactly, but i havent been able to validate it because when i use the url to validate it that gives some html header error and when i copy n paste the code the site freeze n browser not responding – Steph-phpProgrammer Jul 13 '15 at 16:02
  • This is cheeky, however, would you make that file available for us to check? You need to ensure that there is _no sensitive or personal information in it_!!! But we can then see what it actually in it. You need to post a URL to it. Save the file somewhere and make it available as a text file, which is what a JSON file is. – Ryan Vincent Jul 13 '15 at 16:04
  • Are `id` and `area` constants? – frz3993 Jul 13 '15 at 16:07
  • Have you tried `var_dump($mydata);` and comment out the rest to see the json and use a validator to see if it is valid json ? – ODelibalta Jul 13 '15 at 16:12
  • @frz3993 no they are variables i created to store id and area from the json file – Steph-phpProgrammer Jul 13 '15 at 16:13
  • @RyanVincent here is a link to he file https://drive.google.com/file/d/0By-DCRo--qw_bnBFNEJYbkkwTDQ/view?usp=sharing – Steph-phpProgrammer Jul 13 '15 at 16:14
  • Did you look over the answer here: http://stackoverflow.com/questions/4049428/processing-large-json-files-in-php – Twisty Jul 13 '15 at 16:30
  • Also found this to be pretty helpful: http://soyuka.me/streaming-big-json-files-the-good-way/ – Twisty Jul 13 '15 at 16:33
  • I've tested your json file, with your codes and `json_decode` returned null. `json_last_error` returned 4 which translates to malformed JSON. – frz3993 Jul 13 '15 at 17:00
  • @frz3993 so how do you suggest i fix the 'malformed JSON'? and where exactly in the code is this malformed code or error? – Steph-phpProgrammer Jul 13 '15 at 17:45
  • this is the error i keep getting 'Cannot use string offset as an array' when i run the script – Steph-phpProgrammer Jul 13 '15 at 17:56
  • I think that's a different error in your `$id` and `$area` part. The part that produced the malformed error was the `json_decode` part although I tried to validate it on freeformatter and it seems ok. I'm at lost. haha – frz3993 Jul 13 '15 at 18:41

1 Answers1

1

Your JSON file is invalid. Look at line 95053, and look at the "description": "... kind 2 bedroom, 2 1\2 bathroom ..." where you have an unescaped backspace. Escape this (using \\) and it should work.

sisve
  • 19,501
  • 3
  • 53
  • 95