Skip to content

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

json-sorter

A simple utility to sort a JSON object based on keys.

The demo is live here

If you wish to use the sorting in your code, here is the main function that does the trick

function sortJson(json){
   var sortedJson;
   sortedJson = '';
   if (json instanceof Array) {
      sortedJson += '[';
      sortedJson += json.map(function(item) {
         return sortJson(item);
      }).join(',');
      sortedJson += ']';
   } else if (json === null) {
      sortedJson += 'null';
   } else {
      switch (typeof json) {
         case 'undefined':
            sortedJson += 'null';
            break;
         case 'string':
            sortedJson += '"' + json + '"';
            break;
         case 'number':
         case 'boolean':
            sortedJson += json;
            break;
         case 'object':
            sortedJson += '{';
            sortedJson += Object.keys(json).sort().map(function(key) {
               return '"' + key + '":' + sortJson(json[key]);
            }).join(',');
            sortedJson += '}';
            break;
      }
   }
   return sortedJson;
}   

About

A simple utility to sort a JSON object based on keys

Resources

Stars

0 stars

Watchers

1 watching

Forks

Releases

Packages

Used by

Contributors

Languages