1.0.1 ā€¢ Published 2 months ago

sort-jsonc v1.0.1

Weekly downloads
-
License
MIT
Repository
github
Last release
2 months ago

sort-jsonc

npm

āœ… Sort JSONC/JSON5 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 });
{
  // 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

npmyarnpnpm
npm install sort-jsoncyarn add sort-jsoncpnpm add sort-jsonc

API reference

sortJsonc(jsonc, options)

  sortJsonc(jsonc: string, options?: SortJsoncOptions): string

Sorts a JSON/JSONC/JSON5 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

NameTypeDescription
jsoncstringThe JSONC/JSON5 string to sort.
optionsSortJsoncOptionsOptions for sorting. See below for more info.
Options
NameTypeDefaultDescription
sortCompareFn or string[]undefinedCan be a compare function (like Array.sort) or a list of ordered keys. Sorts alphabetically if left blank.
spacesnumber2Number of spaces to indent the JSON. Same as the third parameter of JSON.stringify().
removeCommentsbooleanfalseWhether to remove comments or not.
parseReviverReviverundefinedReviver function, like the second parameter of JSON.parse().
stringifyReviverReviverundefinedReviver function, like the second parameter of JSON.stringify().