3

I am using Nodejs to Create a JSON file from a really large JSON object (1GB). In order to avoid memory problems, I'm using createWriteStream :

var writeStream = fs.createWriteStream('./output/outPut.json')

After using Builder(custom function ) it will return a big object JSON.

the final step is to create this file :

 writeStream.write(JSON.stringify(search_index), (err) => {
             if (err) throw err
             console.log('File  Ready... ')
                    })

but unfortunately, JSON.stringify cannot be used with such heavy Object

JSON.stringify throws RangeError: Invalid string length for huge objects

Is there a solution to this issue, please?

3 Answers3

3

A write stream is of no use if you only write one large string. You cannot use the native JSON.stringify here. There are however quite a few libraries available that will implement JSON serialisation into a stream (and even more for deserialising from a stream), use one of them.

Bergi
  • 630,263
  • 148
  • 957
  • 1,375
0

When writing big collections of data into files (i.e. lots of documents in a search index), it is generally best to work with smaller packages in order to find the balance between memory consumption and computation time.

For example, you could create 100 JavaScript objects (n = 100), stringify them, append them to your file and continue with the next 100 objects.

The trick here is to fine a package size n that makes good use of your system's memory without spending too much time with read/write operations on files.

A common pitfall is that your total number of documents might not be an integer multiple of your package size n. When iterating using a zero-based index variable you should then write to the file if:

index % n === 0 || index === (totalCount - 1)

An additional benefit is that you can start/restart this process after an error without having to recreate all of the data (i.e. if some error occurs after writing 800MB of data you can log the last successful index and continue from there).

Barthy
  • 3,151
  • 1
  • 25
  • 42
0

First step is to init your stream obejct

const writeStream = fs.createWriteStream('./object.json', { flags: 'w' })

then Converting my data to string JSON (stringify ) Using JSON Stream Stringify module

const JsonStreamStringify = require('json-stream-stringify')
const jsonStream = new JsonStreamStringify(Promise.resolve(Promise.resolve(TargetData)))

The last step is to pipe the returned data to writeStream

jsonStream.pipe(writeStream)
jsonStream.on('end', () => console.log('done '))

But It may take a lot of time in my case 10-15 min (1.1GB)

Dharman
  • 30,962
  • 25
  • 85
  • 135