Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

sorted-json

Package Overview
Dependencies
Maintainers
0
Versions
10
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

sorted-json

Sort keys and array elements in a JSON.

  • 0.3.0
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
1.2K
increased by11.9%
Maintainers
0
Weekly downloads
 
Created
Source

sorted-json-js

Sort keys and array elements in a JSON.

Quick Example 1:

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 ]
}

Quick Example 2:

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]}

API:

.sortify(obj [, options])

Sort an object and return it.

ParameterTypeRequiredDefaultDescription
objobjectYesThe object to sort
optionsobject{}Sort options
options.sortByfunctionundefinedSpecifies a function that defines the sort order. Same to compareFunction parameter in Array.prototype.sort(compareFunction)
options.sortKeybooleantrueSort the keys in obj or not
options.sortArraybooleantrueSort the array elements in `obj or not
options.stringifybooleanfalseOutput the stringified obj or not (Using JSON.stringify())
options.replacerParameter for JSON.stringify()
options.spaceParameter for JSON.stringify()

.stringify(obj)

ParameterTypeRequiredDefaultDescription
objobjectYesThe object to sort

Install:

npm install sorted-json

Test:

npm test

License

MIT

Keywords

FAQs

Package last updated on 26 Dec 2024

Did you know?

Socket

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.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc