Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

As the title implies I'm trying to stringify huge JavaScript Object with JSON.stringify in my Node.js app. The objects are - again - huge (tens of mega bytes), they don't contain any functions. I need to write the serialized objects to a file. What I'm getting now is this:

RangeError: Invalid string length   at Object.stringify (native)   at stringifyResult (/my/file.js:123:45) -> line where I use JSON.stringify 

Any idea how to solve that issue?

like image 867
borisdiakur Avatar asked Mar 20 '15 21:03

borisdiakur


People also ask

Is there a limit to JSON Stringify?

So as it stands, a single node process can keep no more than 1.9 GB of JavaScript code, objects, strings, etc combined. That means the maximum length of a string is under 1.9 GB. You can get around this by using Buffer s, which store data outside of the V8 heap (but still in your process's heap).

What happens when you JSON Stringify a string?

The JSON.stringify() method converts a JavaScript value to a JSON string, optionally replacing values if a replacer function is specified or optionally including only the specified properties if a replacer array is specified.

Can JSON Stringify throw an error?

Errors and Edge CasesJSON. stringify() throws an error when it detects a cyclical object. In other words, if an object obj has a property whose value is obj , JSON. stringify() will throw an error.

Can you JSON Stringify an array of objects?

The JSON. stringify method converts a JavaScript object or value to a JSON string. It can optionally modify or filter values if a replacer function/array is specified.


1 Answers

I too have seen this unhelpful/misleading nodejs error message, so I booked an issue over at nodejs github

RangeError: Invalid string length --- it should be saying Out Of Memory

like image 179
Scott Stensland Avatar answered Sep 28 '22 17:09

Scott Stensland