# json-stringify

> Enhanced JSON.stringify

Latest version **1.0.0** (published 2015-10-11) · MIT license · 0 weekly downloads

## Install

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

## Health

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

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.0 |
| Published | 2015-10-11 |
| First published | 2014-06-05 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Node | >=0.10.0 |
| Dependencies | 1 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | kael |
| Maintainers | kael |
| Keywords | json, stringify, indent, offset, enhance |

## Links

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

## Dependencies (1)

- [json-stringify-safe](https://npm.io/package/json-stringify-safe.md) ^5.0.1

## 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

- 1.0.0 (latest) — 2015-10-11
- 0.1.3 — 2014-07-06
- 0.1.2 — 2014-06-05
- 0.1.1 — 2014-06-05
- 0.1.0 — 2014-06-05

## README

# json-stringify [![NPM version](https://badge.fury.io/js/json-stringify.svg)](http://badge.fury.io/js/json-stringify) [![Build Status](https://travis-ci.org/kaelzhang/json-stringify.svg?branch=master)](https://travis-ci.org/kaelzhang/json-stringify) [![Dependency Status](https://gemnasium.com/kaelzhang/json-stringify.svg)](https://gemnasium.com/kaelzhang/json-stringify)

Like JSON.stringify, but enhanced, supports:

- offset for better typo when substituting the stringified json string.
- handle circular object, and doesn't blow up on circular refs.

## Install

```bash
$ npm install json-stringify --save
```

## Usage

```js
var stringify = require('json-stringify');
var array = [1, '2'];

stringify(array, null, 2, {
  offset: 4
});
```

You will get 

```json
[
------1,
------"2"
----]
```

#### We might encounter

So, if we have an template

```
{
  "foo": <bar>,
  "foo2": <bar2>
}
```

And there's an object `obj`

```js
var bar = stringify({
  bee: 'boo'
}, null, 2, {
  offset: 2
});

var bar2 = JSON.stringify({
  bee: 'boo'
}, null, 2);

var obj = {
  bar: bar,
  bar2: bar2
};
```

And the renderered result is:

```js
{
  "foo": {
    "bee": "boo" // well formatted
  },
  "foo2": {
  "bee": "boo" // a little ugly
}
}
```

You must found the difference.

#### Circular Object

```js
var circular = {};
circular.circular = circular;

var stringify = require('json-stringify');
console.log(stringify(circular, null, 2));
```

output:

```
{
  "circular": "[Circular ~]"
}
```


## `stringify(obj, replacer, indent, [options])`

The first three arguments are the same as to JSON.stringify. 

- `options.offset` defines the offset which described above
- `options.decycler` the decycler method of [json-stringify-safe](https://www.npmjs.com/package/json-stringify-safe)

The default decycler function returns the string '[Circular]'. If, for example, you pass in function(k,v){} (return nothing) then it will prune cycles. If you pass in function(k,v){ return {foo: 'bar'}}, then cyclical objects will always be represented as {"foo":"bar"} in the result.

## Licence

MIT
<!-- do not want to make nodeinit to complicated, you can edit this whenever you want. -->

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