Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save BobRupholdt/f1c426ae94959eca1d670bda4e3f0a3b to your computer and use it in GitHub Desktop.
Save BobRupholdt/f1c426ae94959eca1d670bda4e3f0a3b to your computer and use it in GitHub Desktop.
Break large JSON into an array of smaller objects. I made this for Google Apps Script to break large API results into smaller chunks for moving into Firebase. This assumes that each key in the object is a record.
function splitObject_TEST(){Logger.log(splitObject({1:11,2:22,3:33,4:44,5:55,6:66,7:77}, 3))}
function splitObject(data, size){
if(Object.keys(data).length <= size)
return [data];
var count = 0;
var out = [];
for (var key in data) {
var bowl = Math.floor((count++)/size);
if( ! out[bowl] ) out[bowl] = {};
out[bowl][key] = data[key];
}
return out;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment