Jump to content

JSON into CSV : most efficient for large files?


ChenXiu

Recommended Posts

If a json decoded array is huge (1,000,000+ lines) what is the fastest, least CPU intensive way to get it into a CSV?
I only want the ["content"] portion into the .csv file:

// $result = json_decode($jsonFile); print_r($result);
stdClass Object
(
    [content] => Array
        (
            [0] => stdClass Object
                (
                    [id] => 1234
                    [day] => monday
                    [location] => USA
                )
            [1] => stdClass Object
                (
                    [id] => 5678
                    [day] => thursday
                    [location] => EU
                )
        )
    [searchCriteria] => stdClass Object
        (
            [from] => monday
            [toDate] => friday
        )
    [size] => 2
    [number] => 0
)

What is the best way to loop to only get the "id" "day" and "location" from the ["content"] portion of the array in to the .csv file?

$fp = fopen( 'csv_file.csv' , 'w' );
foreach( $result as $i ) { <------- this part.
fputcsv( $fp , $i );
}
fclose($fp);

Thank you.

Link to comment
Share on other sites

7 hours ago, Barand said:

fputcsv() doesn't play nice with objects

Thank you. I've done a lot of benchmarkes and indeed, the fastest speed I could reach was with exactly what you are suggested (using json_decode($result,true) and then looping through $data["content"] ). I thought there might be a way to save 500ms by "capturing" the "content" portion of the json string (using a string function while json is still a string) because string functions always are faster than looping. But maybe not.... I think monkeying with the json with string functions would open a pandoras box of errors 😃

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.