DEV Community

✨Nimmo✨
✨Nimmo✨

Posted on • Edited on

Simplest solution to JSON.stringify RangeError: Invalid string length

We often come across this issue where the object or value passed to JSON.stringify is more than it can handle and the most common solution offered on google is using alternate libraries to stream the data whereas the simplest solution to it is just to stringify one element at a time and concatenate them.

var out="[";
  for(var indx=0;indx<data.length-1;indx++){
    out+=JSON.stringify(data[indx],null,4)+",";
  }
  out+=JSON.stringify(data[data.length-1],null,4)+"]";
Enter fullscreen mode Exit fullscreen mode

You can also save the gist here for future references.
Hope this solution helps you at some point.

Top comments (0)