# json-stringify-pretty-compact

> The best of both `JSON.stringify(obj)` and `JSON.stringify(obj, null, indent)`.

Latest version **4.0.0** (published 2022-05-14) · MIT license · 0 weekly downloads

## Install

```sh
npm install json-stringify-pretty-compact
pnpm add json-stringify-pretty-compact
yarn add json-stringify-pretty-compact
bun add json-stringify-pretty-compact
```

## Health

**Score 45/100 (D)** — status: abandoned.

Positive: has types; esm support; no vulnerabilities; high maintenance score; high quality score.

Warnings: low downloads.

Negative: abandoned.

## Facts

| | |
|---|---|
| Version | 4.0.0 |
| Published | 2022-05-14 |
| First published | 2014-11-01 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM |
| Dependencies | 0 |
| Unpacked size | 7.2 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 269 |
| Author | Simon Lydell |
| Maintainers | lydell |
| Keywords | JSON, stringify, pretty, print, pretty-print, compact, indent, format, formatter |

## Links

- npm: https://www.npmjs.com/package/json-stringify-pretty-compact
- Repository: https://github.com/lydell/json-stringify-pretty-compact
- Homepage: https://github.com/lydell/json-stringify-pretty-compact#readme
- Issues: https://github.com/lydell/json-stringify-pretty-compact/issues
- npm.io page: https://npm.io/package/json-stringify-pretty-compact

## Alternatives

- [@mapbox/jsonlint-lines-primitives](https://npm.io/package/@mapbox/jsonlint-lines-primitives.md) — 5.3M weekly downloads
- [reftools](https://npm.io/package/reftools.md) — 3.5M weekly downloads
- [@hey-api/openapi-ts](https://npm.io/package/@hey-api/openapi-ts.md) — 3.5M weekly downloads
- [@mapbox/geojson-rewind](https://npm.io/package/@mapbox/geojson-rewind.md) — 2.4M weekly downloads
- [turbo-stream](https://npm.io/package/turbo-stream.md) — 1.7M weekly downloads

## Recent versions

- 4.0.0 (latest) — 2022-05-14
- 3.0.0 — 2021-02-20
- 2.0.0 — 2019-02-02
- 1.2.0 — 2018-04-22
- 1.1.0 — 2018-01-12
- 1.0.4 — 2017-04-29
- 1.0.3 — 2017-03-30
- 1.0.2 — 2016-09-08
- 1.0.1 — 2014-11-03
- 1.0.0 — 2014-11-01

## README

# json-stringify-pretty-compact

The output of [JSON.stringify] comes in two flavors: _compact_ and _pretty._ The former is usually too compact to be read by humans, while the latter sometimes is too spacious. This module trades performance for a compromise between the two. The result is a _pretty_ compact string, where “pretty” means both “kind of” and “nice”.

<!-- prettier-ignore -->
```json
{
  "bool": true,
  "short array": [1, 2, 3],
  "long array": [
    {"x": 1, "y": 2},
    {"x": 2, "y": 1},
    {"x": 1, "y": 1},
    {"x": 2, "y": 2}
  ]
}
```

While the “pretty” mode of [JSON.stringify] puts every item of arrays and objects on its own line, this module puts the whole array or object on a single line, unless the line becomes too long (the default maximum is 80 characters). Making arrays and objects multi-line is the only attempt made to enforce the maximum line length; if that doesn’t help then so be it.

## Installation

```
npm install json-stringify-pretty-compact
```

```js
import stringify from "json-stringify-pretty-compact";
```

> **Note:** This is an [ESM only package]. (I haven’t written that gist, but it’s a great resource.)
>
> If you need CommonJS, install version 3.0.0. You won’t be missing out on anything: This package is _done._ No more features will be added, and no bugs have been found in years.

## `stringify(obj, options = {})`

It’s like `JSON.stringify(obj, options.replacer, options.indent)`, except that objects and arrays are on one line if they fit (according to `options.maxLength`).

`options`:

- indent: Defaults to 2. Works exactly like the third parameter of [JSON.stringify].
- maxLength: Defaults to 80. Lines will be tried to be kept at maximum this many characters long.
- replacer: Defaults to undefined. Works exactly like the second parameter of [JSON.stringify].

`stringify(obj, {maxLength: 0, indent: indent})` gives the exact same result as `JSON.stringify(obj, null, indent)`. (However, if you use a `replacer`, integer keys might be moved first.)

`stringify(obj, {maxLength: Infinity})` gives the exact same result as `JSON.stringify(obj)`, except that there are spaces after colons and commas.

**Want more options?** Check out [@aitodotai/json-stringify-pretty-compact]!

## License

[MIT](LICENSE).

[@aitodotai/json-stringify-pretty-compact]: https://www.npmjs.com/package/@aitodotai/json-stringify-pretty-compact
[json.stringify]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify
[esm only package]: https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c

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