Security News
Fluent Assertions Faces Backlash After Abandoning Open Source Licensing
Fluent Assertions is facing backlash after dropping the Apache license for a commercial model, leaving users blindsided and questioning contributor rights.
sorted-json
Advanced tools
Sort keys and array elements in a JSON.
Using .sortify(...)
with { sortArray: true }
options to sort an object:
var sortedJSON = require('sorted-json');
var obj_1 = {
numberArray: [3, 1, 2],
anotherJSON: {
stringArray: ['cat', 'book', 'apple'],
numberA: 2,
numberB: 1
},
number1: 2,
number2: 1
};
console.log(sortedJSON.sortify(obj_1, { sortArray: true }));
Output:
{
anotherJSON: { numberA: 2, numberB: 1, stringArray: [ 'apple', 'book', 'cat' ] },
number1: 2,
number2: 1,
numberArray: [ 1, 2, 3 ]
}
Using .stringify(...)
to get a sorted JSON string:
var obj_2 = {
numberArray: [3, 1, 2],
anotherJSON: {
stringArray: ['cat', 'book', 'apple'],
numberA: 2,
numberB: 1
},
number1: 2,
number2: 1
};
console.log(sortedJSON.stringify(obj_2));
Output:
{"anotherJSON":{"numberA":2,"numberB":1,"stringArray":["cat","book","apple"]},"number1":2,"number2":1,"numberArray":[3,1,2]}
Sort an object and return it.
Parameter | Type | Required | Default | Description |
---|---|---|---|---|
obj | object | Yes | The object to sort | |
options | object | {} | Sort options | |
options.sortBy | function | undefined | Specifies a function that defines the sort order. Same to compareFunction parameter in Array.prototype.sort(compareFunction) | |
options.sortKey | boolean | true | Sort the keys in obj or not | |
options.sortArray | boolean | true | Sort the array elements in `obj or not | |
options.stringify | boolean | false | Output the stringified obj or not (Using JSON.stringify() ) | |
options.replacer | Parameter for JSON.stringify() | |||
options.space | Parameter for JSON.stringify() |
Parameter | Type | Required | Default | Description |
---|---|---|---|---|
obj | object | Yes | The object to sort |
npm install sorted-json
npm test
FAQs
Sort keys and array elements in a JSON.
The npm package sorted-json receives a total of 969 weekly downloads. As such, sorted-json popularity was classified as not popular.
We found that sorted-json demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 0 open source maintainers 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
Fluent Assertions is facing backlash after dropping the Apache license for a commercial model, leaving users blindsided and questioning contributor rights.
Research
Security News
Socket researchers uncover the risks of a malicious Python package targeting Discord developers.
Security News
The UK is proposing a bold ban on ransomware payments by public entities to disrupt cybercrime, protect critical services, and lead global cybersecurity efforts.