a simple utility that strigifies JSON placing the object keys in a sepcific order.
Report Bug
·
Request Feature
Table of Contents
Objects by their nature are an unordered collection of properties. JS engines typically serialize objects in consistent ways. This is perfectly fine for system consumption. However; human readability can be greatly improved by forcing a particular order. The use case arose by the desire to save JSON to disk, review by humans and comparison tools.
- Node.js with npm
Using npm:
npm install @RickZaki/json-stringify-sorted
const jsonStringifySorted = require('json-stringify-sorted');
import * as jsonStringifySorted from 'json-stringify-sorted';
cont sampleObject = {d:[8,6,7,5,3,0,9], a: 1, c:true, b:"b"};
const sortedObject = jsonStringifySorted(sampleObject);
console.log(sortedObject); // {"a":1,"b":"b","c":true,"d":[8,6,7,5,3,0,9]}
AS you can see arrays remain unaltered.
cont sampleObject = {d:{h:"h", e:"e"}, a: 1, c:true, b:"b"};
const sortedObject = jsonStringifySorted(sampleObject);
console.log(sortedObject); // {"a":1,"b":"b","c":true,"d":{"e":"e","h":"h"}}
Nested objects are also sorted
The second optional paramter is used to determine the sort order of the keys.
cont sampleObject = {d:{h:"h", e:"e"}, a: 1, c:true, b:"b"};
const sortedObject = jsonStringifySorted(sampleObject, ['a', 'e', 'd', 'c']);
console.log(sortedObject); // {"a":1,"d":{"e":"e"},"c":true}
a few ccaveats with this use case:
- Keys not provided in the order array are omitted from the result
- The order for root and nested object keys are intertwined. Should a nested object have the same keys, the order will be identical. You cann specify a before b on the ancestor, but b before a on descendant.
As the utility uses Array.sort, you can supply a custom sort compare function. Here's one that forces some keys in order, and the remaining in alphabetical order.
const compareFn = (a, b) =>{
const keyOrder = ['c', 'b'];
const aIndex = keyOrder.indexOf(a);
const bIndex = keyOrder.indexOf(b);
if (aIndex > -1 || bIndex > -1) {
if (aIndex == -1) {
return 1;
} else if (bIndex == -1) {
return -1;
} else if (aIndex < bIndex) {
return -1;
} else {
return 1;
}
}
if (a < b) {
return -1;
} else {
return 1;
}
};
cont sampleObject = {d:{h:"h", e:"e"}, a: 1, c:true, b:"b"};
const sortedObject = jsonStringifySorted(sampleObject, compareFn);
console.log(sortedObject); // {"c":true,"b":"b","a":1,"d":{"e":"e","h":"h"}}
the third optional parameter is an number of spaces to include in the output. Just like JSON.strigify
cont sampleObject = { a: 1, c:true, b:"b"};
const sortedObject = jsonStringifySorted(sampleObject, null, 2);
console.log(sortedObject); /*
{
"a": 1,
"b": "b",
"c": true
}
*/
Contributions are what make the open source community such an amazing place to learn, inspire, and create. Any contributions you make are greatly appreciated.
If you have a suggestion that would make this better, please fork the repo and create a pull request. You can also simply open an issue with the tag "enhancement". Don't forget to give the project a star! Thanks again!
Distributed under the MIT License. See LICENSE
for more information.
Rick Zaki - GitHub-at-RickZaki.com
Project Link: https://github.com/RickZaki/json-stringify-sorted