@rickzaki/json-stringify-sorted
v1.0.2
Published
a variation on JSON.stringify which allows for sorting keys
Downloads
8
Readme
About The Project
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.
Getting Started
Prerequisites
- Node.js with npm
Installation
Using npm:
npm install @RickZaki/json-stringify-sorted
Usage
CommonJS
const jsonStringifySorted = require('json-stringify-sorted');
ECMAScript Modules
import * as jsonStringifySorted from 'json-stringify-sorted';
examples
Simple example
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.
Nested example
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
Key Order
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.
Custom sort function
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"}}
Formatting
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
}
*/
Contributing
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!
License
Distributed under the MIT License. See LICENSE
for more information.
Contact
Rick Zaki - GitHub-at-RickZaki.com
Project Link: https://github.com/RickZaki/json-stringify-sorted