Note that there are some explanatory texts on larger screens.

plurals
  1. POParse large JSON file in Nodejs
    primarykey
    data
    text
    <p>I have a file which stores many JavaScript objects in JSON form and I need to read the file, create each of the objects, and do something with them (insert them into a db in my case). The JavaScript objects can be represented a format:</p> <p><strong>Format A:</strong></p> <pre><code>[{name: 'thing1'}, .... {name: 'thing999999999'}] </code></pre> <p>or <strong>Format B:</strong></p> <pre><code>{name: 'thing1'} // &lt;== My choice. ... {name: 'thing999999999'} </code></pre> <p>Note that the <code>...</code> indicates a lot of JSON objects. I am aware I could read the entire file into memory and then use <code>JSON.parse()</code> like this:</p> <pre><code>fs.readFile(filePath, 'utf-8', function (err, fileContents) { if (err) throw err; console.log(JSON.parse(fileContents)); }); </code></pre> <p>However, the file could be really large, I would prefer to use a stream to accomplish this. The problem I see with a stream is that the file contents could be broken into data chunks at any point, so how can I use <code>JSON.parse()</code> on such objects? </p> <p>Ideally, each object would be read as a separate data chunk, but I am not sure on <em>how to do that</em>.</p> <pre><code>var importStream = fs.createReadStream(filePath, {flags: 'r', encoding: 'utf-8'}); importStream.on('data', function(chunk) { var pleaseBeAJSObject = JSON.parse(chunk); // insert pleaseBeAJSObject in a database }); importStream.on('end', function(item) { console.log("Woot, imported objects into the database!"); });*/ </code></pre> <p>Note, I wish to prevent reading the entire file into memory. Time efficiency does not matter to me. Yes, I could try to read a number of objects at once and insert them all at once, but that's a performance tweak - I need a way that is guaranteed not to cause a memory overload, not matter how many objects are contained in the file. </p> <p>I can choose to use <code>FormatA</code> or <code>FormatB</code> or maybe something else, just please specify in your answer. Thanks!</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload