Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
Writing 500kb of JSON
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  5 messages - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
P. Douglas Reeder  
View profile  
 More options Oct 27 2012, 11:02 pm
From: "P. Douglas Reeder" <reeder...@gmail.com>
Date: Sat, 27 Oct 2012 20:02:13 -0700 (PDT)
Local: Sat, Oct 27 2012 11:02 pm
Subject: Writing 500kb of JSON

One of the things my app needs to do is write a large JSON file to disk.
 Currently, it's implemented naively:

writeStream = fs.createWriteStream(process.cwd() + "/staticRoot/album.json",
{"encoding": "utf8"});

writeStream.addListener("error", function (error) {

console.error("album.json:", error);

if (! writeError)   // preserve 1st error

writeError = error;

callback(writeError);   // on error, abort writing next file

});

 writeStream.end(JSON.stringify(album));

This appears to work ok when the JSON file is 150k (the largest size I can
readily test).  It needs to work when the JSON file is up to 500k.  i'm not
sure how big the write buffer is, but I lack confidence that this is the
right way to go.  The bulk of the JSON file is an array of objects, so I
could create the JSON file one array item at a time, checking the return
value from writeStream.write() in a manner analogous to the following HTML
file writing:

writeStream.addListener("drain", writeUntilBufferFull);

 writeStream.write("<h1>" + title + "</h1>\n");

writeStream.write("<table>\n");

function writeUntilBufferFull() {

var longDate, urlFileName, htmlFileName, row;

console.log("writeUntilBufferFull()   p=", p, "   metadata:", JSON.stringify
(pictureMetadata[p]));

while (p < pictureMetadata.length) {

longDate = pictureMetadata[p].date ? pictureMetadata[p].date.
toLocaleDateString() : "";

urlFileName = encodeURIComponent(pictureMetadata[p].fileName).replace(/'/g,
'%27');

htmlFileName = pictureMetadata[p].fileName.replace(/&/g, "&amp;").replace(
/</g, "&lt;").replace(/>/g, "&gt;").replace(/"/g, "&quot;");

row =  " <tr><td align='right'>" + (p+1) + "</td><td align='right'>" +longDate
+

"</td><td><a class='gallery' href='pictures/" + urlAlbumName + "/" +urlFileName
+ "' title='" + longDate + "'>" + htmlFileName + "</a></td>" +

"<td><a download=\"" + htmlFileName + "\" href='full-size/" + urlAlbumName +
"/" + urlFileName + "'>full-size</a></td></tr>\n";

if (! writeStream.write(row))

 break;

++p;

}

 if (p < pictureMetadata.length) {

++p;

} else {

writeStream.removeListener("drain", writeUntilBufferFull);

 writeStream.end();

 callback(writeError);

}
}

What's the best strategy for writing large JSON files?

 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
mscdex  
View profile  
 More options Oct 27 2012, 11:09 pm
From: mscdex <msc...@gmail.com>
Date: Sat, 27 Oct 2012 20:09:04 -0700 (PDT)
Local: Sat, Oct 27 2012 11:09 pm
Subject: Re: Writing 500kb of JSON
On Oct 27, 11:02 pm, "P. Douglas Reeder" <reeder...@gmail.com> wrote:

> What's the best strategy for writing large JSON files?

You could look into using a streaming JSON module. Here's one you
might try: https://github.com/dominictarr/JSONStream

 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
dolphin278  
View profile  
 More options Oct 28 2012, 1:30 am
From: dolphin278 <dolphin...@gmail.com>
Date: Sun, 28 Oct 2012 09:29:55 +0400
Local: Sun, Oct 28 2012 1:29 am
Subject: Re: [nodejs] Writing 500kb of JSON

Check https://github.com/dominictarr/JSONStream

On 28.10.2012, at 7:02, "P. Douglas Reeder" <reeder...@gmail.com> wrote:


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
P. Douglas Reeder  
View profile  
 More options Oct 28 2012, 10:40 am
From: "P. Douglas Reeder" <reeder...@gmail.com>
Date: Sun, 28 Oct 2012 07:40:15 -0700 (PDT)
Local: Sun, Oct 28 2012 10:40 am
Subject: Re: [nodejs] Writing 500kb of JSON

Thanks! I'll give JSONStream a try.


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Ben Noordhuis  
View profile  
 More options Oct 29 2012, 7:49 am
From: Ben Noordhuis <i...@bnoordhuis.nl>
Date: Mon, 29 Oct 2012 12:49:04 +0100
Local: Mon, Oct 29 2012 7:49 am
Subject: Re: [nodejs] Writing 500kb of JSON
On Sun, Oct 28, 2012 at 4:02 AM, P. Douglas Reeder <reeder...@gmail.com> wrote:

Depends on your definition of 'large' and how often you need to write
that data.  If it's just a one-off or relatively rare thing, use
fs.writeFile() and call it a day, i.e.:

  var data = JSON.stringify(obj);
  fs.writeFile('/path/to/file', data, 'utf8', cb);


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »