5

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?

5
  • whats the purpose of creating such a large file. Can you split the information into smaller files? Commented Dec 20, 2020 at 21:35
  • @Craicerjack I am using elasticlunr JS to build the search module in my application, and this file is the search index, unfortunately, it cannot be split into multiple files Commented Dec 20, 2020 at 21:38
  • 1
    Does this answer your question? Parse large JSON file in Nodejs Commented Dec 20, 2020 at 21:38
  • @RishabhDeepSingh this answer is about how to read large JSON not create it Commented Dec 20, 2020 at 21:40
  • Is searchIndex an array or object? Commented Dec 20, 2020 at 22:54

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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)

Comments

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).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.