🚀 Big News: Socket Acquires Coana to Bring Reachability Analysis to Every Appsec Team.Learn more
Socket
DemoInstallSign in
Socket

sort-json-keys

Package Overview
Dependencies
Maintainers
1
Versions
6
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

sort-json-keys

Sort JSON keys alphabetically at all levels

1.0.3
latest
Source
npm
Version published
Weekly downloads
331
3.44%
Maintainers
1
Weekly downloads
 
Created
Source

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

Keywords

sort

FAQs

Package last updated on 14 Jun 2021

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