5

I'm trying to json.stringify a 1 gb object so that I can write it to disk. I get FATAL ERROR: JS Allocation failed - process out of memory

What can I do to stringify successfully?

1

2 Answers 2

5

You can stringify bit by bit manually: if y is a key of x, then JSON.stringify(y) + ":" + JSON.stringify(x[y]) gives you one segment.

Using fs.appendFileSync, for example, you can use:

var output = "out.json";
fs.writeFileSync(output, "{");
var first = true;
for(var y in x) {
    if(x.hasOwnProperty(y)) {
        if(first) first = false;
        else fs.appendFileSync(output, ",");
        fs.appendFileSync(output, JSON.stringify(y) + ":" + JSON.stringify(x[y]))
    }
} 
fs.appendFileSync(output, "}");

You can also use a Write Stream

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

Comments

0

Extended with Object, Array checker

var first, y, i$, ref$, len$, toString$ = {}.toString;
switch (toString$.call(x).slice(8, -1)) {
case 'Object':
  fs.writeFileSync(output, '{');
  first = true;
  for (y in x) {
    if (x.hasOwnProperty(y)) {
      if (first) {
        first = false;
      } else {
        fs.appendFileSync(output, ',');
      }
      fs.appendFileSync(output, JSON.stringify(y) + ':' + JSON.stringify(x[y]));
    }
  }
  fs.appendFileSync(output, '}');
  break;
case 'Array':
  fs.writeFileSync(output, '[');
  first = true;
  for (i$ = 0, len$ = (ref$ = x).length; i$ < len$; ++i$) {
    y = ref$[i$];
    if (first) {
      first = false;
    } else {
      fs.appendFileSync(output, ',');
    }
    fs.appendFileSync(output, JSON.stringify(y));
  }
  fs.appendFileSync(output, ']');
  break;
default:
  fs.writeFileSync(output, JSON.stringify(x));
}

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.