sort-json-keys
TypeScript icon, indicating that this package has built-in type declarations

1.0.3 • Public • Published

sort-json-keys

NPM version dependencies: none code style: prettier Conventional Commits License MIT

Sort JSON keys alphabetically at all levels (for example for sorting package-lock.json).

Install

Requires node@14 or higher:

npm install sort-json-keys --save-dev

Usage

CLI

Sort all keys in the foo.json file in alphabetical order, indented by two spaces (default indentation):

npx sort-json-keys foo.json

Sorting the keys does not change the JS value of the JSON. In particular, array elements are not sorted.

For example this converts a JSON-file

{ "b": 1, "a": { "y": 2, "x": 3 }, "c": ["b", "a"] }

to a file

{
  "a": {
    "x": 3,
    "y": 2
  },
  "b": 1,
  "c": [
    "b",
    "a"
  ]
}

Sort all keys in foo.json and baz/bar.json files in alphabetical order, indented by two spaces:

npx sort-json-keys foo.json baz/bar.json

Sort all keys in foo.json and bar.json files, indented by four spaces:

npx sort-json-keys foo.json bar.json --indent 4

Sort all keys in foo.json files without indentation (as a one-line file):

npx sort-json-keys foo.json --indent 0

JavaScript/TypeScript API

import sortJsonKeys from "sort-json-keys";
// or
import { sortJsonKeys } from "sort-json-keys";

const json = { b: 1, a: 2 };

const sortedJson = sortJsonKeys(json); // { a: 2, b: 1 }

License

MIT

/sort-json-keys/

    Package Sidebar

    Install

    npm i sort-json-keys

    Weekly Downloads

    308

    Version

    1.0.3

    License

    MIT

    Unpacked Size

    5.46 kB

    Total Files

    5

    Last publish

    Collaborators

    • uid-11222