2

I have a large json object (174MB) https://www.dropbox.com/s/q3dpubc2emwrnyq/attributes-nodp.json?dl=0

I want to save the json object as decoded associative PHP array in a file for later reference/inclusion.

<?php $json_data = file_get_contents('attributes-nodp.json'); //This will retrieve your json data as a string
$arr = json_decode($json_data, true); //This will decode your string data into a PHP array; the true parameter makes it an associative array

$file = fopen("/home/kalugi/public_html/wp-content/uploads/wpallimport/files/test/raw_attributes.php", "w");
//This will open a new file raw_attributes.php for writing

//This will write and close the file
fwrite($file  , $arr->__toString());
     fclose($file );
?>

However this throws an error

[23-Dec-2019 12:59:46 UTC] PHP Fatal error:  Uncaught Error: Call to a member function __toString() on array in /home/kalugi/public_html/wp-content/uploads/wpallimport/files/test/write-array-to-file.php:8
Stack trace:
#0 {main}
  thrown in /home/kalugi/public_html/wp-content/uploads/wpallimport/files/test/write-array-to-file.php on line 8

I then tried var_export to create a string that could be saved to the file and that didn't produce a file with anything.

<?php
$json_data = file_get_contents('attributes-nodp.json'); //This will retrieve your json data as a string
$arr = var_export(json_decode($json_data, true)); //This will decode your string data into a PHP array; the true parameter makes it an associative array
$file = fopen("/home/kalugi/public_html/wp-content/uploads/wpallimport/files/test/raw_attributes.php", "w");
//This will open a new file raw_attributes.php for writing
//This will write and close the file
fwrite($file, $arr->__toString());
fclose($file);
?>

Please advise.

ptrcao
  • 421
  • 1
  • 5
  • 19
  • This might help you https://stackoverflow.com/a/7895384/1483629 – Vinay Patil Dec 23 '19 at 13:17
  • Would you be better off to import this data into a database? – Nigel Ren Dec 23 '19 at 13:18
  • What is the objective? Preserving previous and storing it with new one OR directly storing the new one? – nice_dev Dec 23 '19 at 13:19
  • @vivek_23 I don't really need the json object as it's the wrong format for what I need. I want to convert the json object into a a PHP array because I want to do some PHP operations on this data as a PHP array. I figure I could just have the PHP array handy in a file for direct access and inclusion in a script. It's a very big array though (174MB). – ptrcao Dec 23 '19 at 13:26
  • @NigelRen maybe. I don't know anything about that though, so unless you have a very simple solution in mind that doesn't assume too much database know-how. – ptrcao Dec 23 '19 at 13:29
  • @ptrcao Then do the processing after your `json_decode` step. Where are you stuck? To store an array into a file, you can store it in JSON format or in a serialized way. – nice_dev Dec 23 '19 at 13:31
  • @vivek_23 Perhaps I will then. Though, for educational purposes, does anyone know why I got that error? Is it a syntactical error, or a mis-application of any of the PHP functions above? – ptrcao Dec 23 '19 at 13:37
  • 2
    @ptrcao `Uncaught Error: Call to a member function __toString() on array` came because you are calling a method `__toString()` on an array variable and not on an object of some class which has a `__toString()` method. – nice_dev Dec 23 '19 at 13:43
  • 1
    @vivek_23, as they are reading a JSON and decoding it - not sure why they would want to store it back to a file as JSON. – Nigel Ren Dec 23 '19 at 13:45
  • @NigelRen Yes, I am unsure too. Maybe, he wants to avoid an API call to dropbox again. – nice_dev Dec 23 '19 at 13:47

1 Answers1

2

It doesn't work because you're trying to invoke a __toString method on something that is not an object that has a __toString method (its an assoc. array).

If you want to save the var_export output of your data, then you have to pass true as a second parameter(to return the data instead of echoing it):

file_put_contents(PATH_TO_FILE, var_export(json_decode($json_data,true),true));

If you want to save the actual data for future use then it IMO it would be best to either parse the file and save it to a database or just save the JSON file and use that.

  • Please add proper error handling and/or use `file_put_contents()` instead – Marcin Orlowski Dec 23 '19 at 15:07
  • @Marcin Orlowski Changed it to use file_put_contents, not sure that this change is a good one since the initial answer's code was exactly mirroring OP's(just without the error), but whatever. – SessionCookieMonster Dec 23 '19 at 15:19
  • The reason I didn't originally use `file_put_contents()` as compared to `fopen` was because I was dissuaded by a point about performance - [here](https://stackoverflow.com/questions/1768894/how-to-write-into-a-file-in-php#comment67259150_1768944) by John. What do you guys make of it? – ptrcao Dec 23 '19 at 16:23
  • You can stick to old fopen() if you want but it then needs error handling. – Marcin Orlowski Dec 23 '19 at 17:09