var recursivelyOrderKeys = function(unordered) { // If it's an array - recursively order any // dictionary items within the array if (Array.isArray(unordered)) { unordered.forEach(function (item, index) { unordered[index] = recursivelyOrderKeys(item); }); return unordered; } // If it's an object - let's order the keys if (typeof unordered === 'object') { var ordered = {}; Object.keys(unordered).sort().forEach(function(key) { ordered[key] = recursivelyOrderKeys(unordered[key]); }); return ordered; } return unordered; }; var stringifyKeysInOrder = function(data) { var sortedData = recursivelyOrderKeys(data); return JSON.stringify(sortedData, null, ' '); };