DEV Community

loading...

Simplest solution to JSON.stringify RangeError: Invalid string length

madhunimmo profile image ✨Nimmo✨ Updated on ・1 min read

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

Hope this solution helps you at some point.

Discussion (0)

pic
Editor guide