sort-jsonc
v1.0.2
Published
Sort JSONC without mangling comments!
Downloads
92
Maintainers
Readme
sort-jsonc
✅ Sort JSONC (JSON with comments) without mangling comments!
Works with regular JSON too, of course!
See sort-jsonc-cli for the CLI version.
Usage
See the API reference for info on all options.
import { sortJsonc } from 'sort-jsonc';
// JSON with comments
const jsonc = `{
"charlie": 0,
/*
* Big block comment explaining "nested"
*/
"nested": {
"caesar": 0, // Comment left of "nested.c"
"adam": 0,
"bertil": 0
},
"array": [
{ "amsterdam": 0, "baltimore": 0 },
{ "yankee": 0, "zulu": 0 }
],
"bravo": 0,
// Comment above "a"
"alfa": 1
}`;
// Sort it alphabetically...
const sortedAlphabetically = sortJsonc(jsonc);
// ... or sort it by preferred key order...
const sortedPreferred = sortJsonc(jsonc, { sort: ['nested', 'array'] });
// ... or sort it however you want!
const sortedByKeyLength = sortJsonc(jsonc, { sort: (a, b) => a.length - b.length });
sortedAlphabetically
{
// Comment above "a"
"alfa": 1,
"array": [
{
"amsterdam": 0,
"baltimore": 0
},
{
"yankee": 0,
"zulu": 0
}
],
"bravo": 0,
"charlie": 0,
/*
* Big block comment explaining "nested"
*/
"nested": {
"adam": 0,
"bertil": 0,
"caesar": 0 // Comment left of "nested.c"
}
}
sortedPreferred
{
/*
* Big block comment explaining "nested"
*/
"nested": {
"adam": 0,
"bertil": 0,
"caesar": 0 // Comment left of "nested.c"
},
"array": [
{
"amsterdam": 0,
"baltimore": 0
},
{
"yankee": 0,
"zulu": 0
}
],
// Comment above "a"
"alfa": 1,
"bravo": 0,
"charlie": 0
}
sortedAlphabetically
{
// Comment above "a"
"alfa": 1,
"array": [
{
"amsterdam": 0,
"baltimore": 0
},
{
"zulu": 0,
"yankee": 0
}
],
"bravo": 0,
/*
* Big block comment explaining "nested"
*/
"nested": {
"adam": 0,
"caesar": 0, // Comment left of "nested.c"
"bertil": 0
},
"charlie": 0
}
Installation
| npm | yarn | pnpm |
| ------------------------ | --------------------- | --------------------- |
| npm install sort-jsonc
| yarn add sort-jsonc
| pnpm add sort-jsonc
|
API reference
sortJsonc(jsonc, options)
sortJsonc(jsonc: string, options?: SortJsoncOptions): string
Sorts a JSON/JSONC string without mangling comments (can also remove them if wanted).
Sorts alphabetically by default, but can also sort by preferred key order or by a custom sort function.
Parameters
| Name | Type | Description |
| --------- | ------------------ | ------------------------------------------------------------------ |
| jsonc
| string
| The JSONC string to sort. |
| options
| SortJsoncOptions
| Options for sorting. See below for more info. |
Options
| Name | Type | Default | Description |
| ------------------ | ------------------------- | ----------- | ------------------------------------------------------------------------------------------------------------ |
| sort
| CompareFn
or string[]
| undefined
| Can be a compare function (like Array.sort
) or a list of ordered keys. Sorts alphabetically if left blank. |
| spaces
| number
| 2
| Number of spaces to indent the JSON. Same as the third parameter of JSON.stringify()
. |
| removeComments
| boolean
| false
| Whether to remove comments or not. |
| parseReviver
| Reviver
| undefined
| Reviver function, like the second parameter of JSON.parse()
. |
| stringifyReviver
| Reviver
| undefined
| Reviver function, like the second parameter of JSON.stringify()
. |