phpmysqljsonwordpress

Export MySQL table rows as individual files (specifically JSON) in one go


I have over 750 JSON files I need to create from a MySQL Database table.

It is the WordPress "wp_options" table, but this is a MySQL question.

The wp_options table has the following properties.

option_id, option_name, option_value, autoload

The "option_name" is to become the JSON file name.

I am fine if I "have to" rename each file name manually.

The "option_value" is to become the JSON data.

Is there a way I can do this more efficiently instead of creating an empty JSON file for each row and then copying the data base option_value to the JSON file?

My main concern is with 750 files to make I am a little weary I will miss something or double up on something, and this information has to be exact.


Solution

  • If I needed to do this, and only needed to do it once, I'd probably just run a little php script locally.

    Assuming you have grabbed this table as an array (here I've called it $wp_options), you could just iterate over it using fopen, fwrite and fclose to make your files. I've also assumed you want the files to have '.json' extensions but obviously you can strip that out.

    foreach ($wp_options as $wpo) {
        $newFile = fopen($wpo['option_name'].'.json', 'w'); // w=write mode
        fwrite($newFile, json_encode($wpo['option_value']));
        fclose($newFile);
    }  
    

    The above is untested, but I think that would work.