# sort-json

> Takes a json-file and return a copy of the same file, but sorted

Latest version **2.0.1** (published 2022-02-12) · MIT license · 0 weekly downloads

## Install

```sh
npm install sort-json
pnpm add sort-json
yarn add sort-json
bun add sort-json
```

Provides the command `sort-json`.

## Health

**Score 23/100 (F)** — status: abandoned.

Positive: has types package; no vulnerabilities; high quality score.

Warnings: low downloads; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 2.0.1 |
| Published | 2022-02-12 |
| First published | 2012-11-16 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | separate (@types/sort-json) |
| Module format | CommonJS |
| Dependencies | 3 |
| Unpacked size | 22 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 73 |
| Author | David Björklund |
| Maintainers | kesla |

## Links

- npm: https://www.npmjs.com/package/sort-json
- Repository: https://github.com/kesla/sort-json
- Issues: https://github.com/kesla/sort-json/issues
- npm.io page: https://npm.io/package/sort-json

## Dependencies (3)

- [minimist](https://npm.io/package/minimist.md) ^1.2.0
- [detect-indent](https://npm.io/package/detect-indent.md) ^5.0.0
- [detect-newline](https://npm.io/package/detect-newline.md) ^2.1.0

## Recent versions

- 2.0.1 (latest) — 2022-02-12
- 2.0.0 — 2018-06-16
- 1.6.1 — 2018-01-12
- 1.6.0 — 2017-11-30
- 1.5.2 — 2017-09-26
- 1.5.1 — 2017-09-25
- 1.5.0 — 2017-09-24
- 1.4.1 — 2017-04-19
- 1.4.0 — 2016-09-08
- 1.3.0 — 2016-07-30
- 1.2.0 — 2016-03-16
- 1.1.1 — 2015-10-01
- 1.1.0 — 2015-02-13
- 1.0.0 — 2015-02-13
- 0.0.1 — 2012-11-16

## README

# sort-json [![Build Status](https://travis-ci.org/kesla/sort-json.svg?branch=master)](https://travis-ci.org/kesla/sort-json)

It takes a JSON file and returns a copy of the same file, but with the sorted keys.

## Installation

` [sudo] npm -g install sort-json`

## Usage

```js
const sortJson = require('sort-json');

const options = { ignoreCase: true, reverse: true, depth: 1};
const copy = sortJson({ AA: 123, a: 1, b: 21 }, options);
// copy => { b: 21, AA: 123, a: 1 }

sortJson.overwrite('some/absolute/path.json', options);
// sorts the json at absolute path and overwrites file, also returns sorted object

sortJson.overwrite(['some/absolute/path1.json', 'some/absolute/path2.json'], options);
// sorts the json at absolute paths and overwrites files, also returns array of sorted objects
```

## CLI usage

`sort-json filename [options]`
Sorts and overwrites .json or .rc files.

_Example_
`sort-json test.json --ignore-case`

 **Options**

`--ignore-case, -i`\
Ignore case when sorting.

`--reverse, -r`\
Reverse the ordering z -> a

`--depth=DEPTH, -d`\
The sorting _DEPTH_ on multidimensional objects.
Use a number greater then 0 for the _DEPTH_ value.

`--indent-size=SIZE, --spaces=SIZE`\
Formats the file content with an indentation of _SIZE_ spaces  (default: detects the used indentation of the file).
Use a number greater then 0 for the _SIZE_ value.

`--no-final-newline, -nn`\
No final new line will be added to the end of the file.


## Upgrade to version 2.x

sort-json 2.0.0 will create a different output when the source JSON file does not use an indent size of 2 spaces.
Use `--indent-size=2` to always create an output file with 2 spaces.

## Tests

`npm test`

---
_Source: https://npm.io/package/sort-json · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
