Research
Security News
Malicious npm Packages Inject SSH Backdoors via Typosquatted Libraries
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.
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 options = { ignoreCase: true, reverse: true, depth: 1};
const copy = sortJson({ AA: 123, a: 1, b: 21 }, options);
// copy => { b: 21, AA: 123, a: 1 }
sortJson.overwrite('some/absolute/path.json', options);
// sorts the json at absolute path and overwrites file, also returns sorted object
sortJson.overwrite(['some/absolute/path1.json', 'some/absolute/path2.json'], options);
// sorts the json at absolute paths and overwrites files, also returns array of sorted objects
sort-json filename [options]
Sorts and overwrites .json or .rc files.
Example
sort-json test.json --ignore-case
Options
--ignore-case, -i
Ignore case when sorting.
--reverse, -r
Reverse the ordering z -> a
--depth=DEPTH, -d
The sorting DEPTH on multidimensional objects.
Use a number greater then 0 for the DEPTH value.
--indent-size=SIZE, --spaces=SIZE
Formats the file content with an indentation of SIZE spaces (default: detects the used indentation of the file).
Use a number greater then 0 for the SIZE value.
--no-final-newline, -nn
No final new line will be added to the end of the file.
sort-json 2.0.0 will create a different output when the source JSON file does not use an indent size of 2 spaces.
Use --indent-size=2
to always create an output file with 2 spaces.
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.
Research
Security News
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.
Security News
MITRE's 2024 CWE Top 25 highlights critical software vulnerabilities like XSS, SQL Injection, and CSRF, reflecting shifts due to a refined ranking methodology.
Security News
In this segment of the Risky Business podcast, Feross Aboukhadijeh and Patrick Gray discuss the challenges of tracking malware discovered in open source softare.