Instead of creating JSON in a for-loop, create a regular JavaScript object using your for-loops, and use JSON.stringify (myObject) to create JSON.
var myObject = {}; for(...) { myObject.property = 'newValue'; myObject.anotherProp = []; for(...) { myObject.anotherProp.push('somethingElse'); } } var json = JSON.stringify(myObject);
source share