Security News
Weekly Downloads Now Available in npm Package Search Results
Socket's package search now displays weekly downloads for npm packages, helping developers quickly assess popularity and make more informed decisions.
The sort-json npm package is a utility for sorting the keys of a JSON object. It helps in maintaining a consistent order of keys, which can be useful for readability, comparison, and version control.
Sort JSON keys alphabetically
This feature sorts the keys of a JSON object in alphabetical order. The code sample demonstrates how to use the sort-json package to sort an unsorted JSON object.
const sortJson = require('sort-json');
const unsortedJson = { "b": 2, "a": 1, "c": 3 };
const sortedJson = sortJson(unsortedJson);
console.log(sortedJson); // { "a": 1, "b": 2, "c": 3 }
Sort JSON keys with custom comparator
This feature allows sorting the keys of a JSON object using a custom comparator function. The code sample shows how to sort the keys in reverse alphabetical order.
const sortJson = require('sort-json');
const unsortedJson = { "b": 2, "a": 1, "c": 3 };
const sortedJson = sortJson(unsortedJson, { comparator: (a, b) => b.localeCompare(a) });
console.log(sortedJson); // { "c": 3, "b": 2, "a": 1 }
Sort nested JSON objects
This feature sorts the keys of nested JSON objects. The code sample demonstrates how to sort a JSON object with nested objects.
const sortJson = require('sort-json');
const unsortedJson = { "b": { "d": 4, "c": 3 }, "a": 1 };
const sortedJson = sortJson(unsortedJson, { depth: null });
console.log(sortedJson); // { "a": 1, "b": { "c": 3, "d": 4 } }
The sort-keys package sorts the keys of an object in a similar way to sort-json. It offers options for deep sorting and custom comparators. However, sort-keys is more focused on general object key sorting rather than JSON-specific use cases.
The json-stable-stringify package provides a way to stringify JSON objects with stable key ordering. It ensures that the keys are sorted consistently, which is useful for comparing JSON objects. Unlike sort-json, it focuses on producing a stable string representation rather than directly manipulating JSON objects.
The deep-sort-object package sorts the keys of an object deeply, similar to sort-json. It is useful for ensuring consistent key order in deeply nested objects. However, it does not offer as many customization options as sort-json.
It takes a JSON file and returns a copy of the same file, but with the sorted keys.
[sudo] npm -g install sort-json
const sortJson = require('sort-json');
const options1 = { ignoreCase: true, reverse: true, depth: 1 };
const copy = sortJson({ AA: 123, a: 1, b: 21 }, options1);
// copy => { b: 21, AA: 123, a: 1 }
sortJson.overwrite('some/absolute/path.json');
// sorts the json at absolute path and overwrites file, also returns sorted object
sortJson.overwrite(['some/absolute/path1.json', 'some/absolute/path2.json']);
// sorts the json at absolute paths and overwrites files, also returns array of sorted objects
sort-json file.json
=> sorts and overwrites file.json
-i
or --ignore-case
to ignore case when sorting.
-r
or --reverse
to reverse order z -> a
-d
or --depth
to chose the sorting depth on multidimensional objects
npm test
FAQs
Takes a json-file and return a copy of the same file, but sorted
The npm package sort-json receives a total of 187,695 weekly downloads. As such, sort-json popularity was classified as popular.
We found that sort-json demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Socket's package search now displays weekly downloads for npm packages, helping developers quickly assess popularity and make more informed decisions.
Security News
A Stanford study reveals 9.5% of engineers contribute almost nothing, costing tech $90B annually, with remote work fueling the rise of "ghost engineers."
Research
Security News
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.