[d24f17c] | 1 | # json-stable-stringify <sup>[![Version Badge][npm-version-svg]][package-url]</sup>
|
---|
| 2 |
|
---|
| 3 | [![github actions][actions-image]][actions-url]
|
---|
| 4 | [![coverage][codecov-image]][codecov-url]
|
---|
| 5 | [![License][license-image]][license-url]
|
---|
| 6 | [![Downloads][downloads-image]][downloads-url]
|
---|
| 7 |
|
---|
| 8 | [![npm badge][npm-badge-png]][package-url]
|
---|
| 9 |
|
---|
| 10 | deterministic version of `JSON.stringify()` so you can get a consistent hash from stringified results
|
---|
| 11 |
|
---|
| 12 | You can also pass in a custom comparison function.
|
---|
| 13 |
|
---|
| 14 | [](https://ci.testling.com/ljharb/json-stable-stringify)
|
---|
| 15 |
|
---|
| 16 | [](http://travis-ci.org/ljharb/json-stable-stringify)
|
---|
| 17 |
|
---|
| 18 | # example
|
---|
| 19 |
|
---|
| 20 | ``` js
|
---|
| 21 | const stringify = require('json-stable-stringify');
|
---|
| 22 |
|
---|
| 23 | const obj = { c: 8, b: [{ z: 6, y: 5, x: 4 }, 7], a: 3 };
|
---|
| 24 |
|
---|
| 25 | console.log(stringify(obj));
|
---|
| 26 | ```
|
---|
| 27 |
|
---|
| 28 | output:
|
---|
| 29 |
|
---|
| 30 | ```
|
---|
| 31 | {"a":3,"b":[{"x":4,"y":5,"z":6},7],"c":8}
|
---|
| 32 | ```
|
---|
| 33 |
|
---|
| 34 | # methods
|
---|
| 35 |
|
---|
| 36 | ``` js
|
---|
| 37 | const stringify = require('json-stable-stringify')
|
---|
| 38 | ```
|
---|
| 39 |
|
---|
| 40 | <a id="var-str--stringifyobj-opts"></a>
|
---|
| 41 | ## const str = stringify(obj, opts)
|
---|
| 42 |
|
---|
| 43 | Return a deterministic stringified string `str` from the object `obj`.
|
---|
| 44 |
|
---|
| 45 | ## options
|
---|
| 46 |
|
---|
| 47 | ### cmp
|
---|
| 48 |
|
---|
| 49 | If `opts` is given, you can supply an `opts.cmp` to have a custom comparison function for object keys.
|
---|
| 50 | Your function `opts.cmp` is called with these parameters:
|
---|
| 51 |
|
---|
| 52 | ``` js
|
---|
| 53 | opts.cmp({ key: akey, value: avalue }, { key: bkey, value: bvalue }, { get(key): value })
|
---|
| 54 | ```
|
---|
| 55 |
|
---|
| 56 | For example, to sort on the object key names in reverse order you could write:
|
---|
| 57 |
|
---|
| 58 | ``` js
|
---|
| 59 | const stringify = require('json-stable-stringify');
|
---|
| 60 |
|
---|
| 61 | const obj = { c: 8, b: [{ z: 6, y: 5, x: 4 },7], a: 3 };
|
---|
| 62 |
|
---|
| 63 | const s = stringify(obj, function (a, b) {
|
---|
| 64 | return b.key.localeCompare(a.key);
|
---|
| 65 | });
|
---|
| 66 |
|
---|
| 67 | console.log(s);
|
---|
| 68 | ```
|
---|
| 69 |
|
---|
| 70 | which results in the output string:
|
---|
| 71 |
|
---|
| 72 | ``` js
|
---|
| 73 | {"c":8,"b":[{"z":6,"y":5,"x":4},7],"a":3}
|
---|
| 74 | ```
|
---|
| 75 |
|
---|
| 76 | Or if you wanted to sort on the object values in reverse order, you could write:
|
---|
| 77 |
|
---|
| 78 | ``` js
|
---|
| 79 | const stringify = require('json-stable-stringify');
|
---|
| 80 |
|
---|
| 81 | const obj = { d: 6, c: 5, b: [{ z: 3, y: 2, x: 1 }, 9], a: 10 };
|
---|
| 82 |
|
---|
| 83 | const s = stringify(obj, function (a, b) {
|
---|
| 84 | return a.value < b.value ? 1 : -1;
|
---|
| 85 | });
|
---|
| 86 |
|
---|
| 87 | console.log(s);
|
---|
| 88 | ```
|
---|
| 89 |
|
---|
| 90 | which outputs:
|
---|
| 91 |
|
---|
| 92 | ``` js
|
---|
| 93 | {"d":6,"c":5,"b":[{"z":3,"y":2,"x":1},9],"a":10}
|
---|
| 94 | ```
|
---|
| 95 |
|
---|
| 96 | An additional param `get(key)` returns the value of the key from the object being currently compared.
|
---|
| 97 |
|
---|
| 98 | ### space
|
---|
| 99 |
|
---|
| 100 | If you specify `opts.space`, it will indent the output for pretty-printing.
|
---|
| 101 | Valid values are strings (e.g. `{space: \t}`) or a number of spaces
|
---|
| 102 | (`{space: 3}`).
|
---|
| 103 |
|
---|
| 104 | For example:
|
---|
| 105 |
|
---|
| 106 | ```js
|
---|
| 107 | const obj = { b: 1, a: { foo: 'bar', and: [1, 2, 3] } };
|
---|
| 108 |
|
---|
| 109 | const s = stringify(obj, { space: ' ' });
|
---|
| 110 |
|
---|
| 111 | console.log(s);
|
---|
| 112 | ```
|
---|
| 113 |
|
---|
| 114 | which outputs:
|
---|
| 115 |
|
---|
| 116 | ```
|
---|
| 117 | {
|
---|
| 118 | "a": {
|
---|
| 119 | "and": [
|
---|
| 120 | 1,
|
---|
| 121 | 2,
|
---|
| 122 | 3
|
---|
| 123 | ],
|
---|
| 124 | "foo": "bar"
|
---|
| 125 | },
|
---|
| 126 | "b": 1
|
---|
| 127 | }
|
---|
| 128 | ```
|
---|
| 129 |
|
---|
| 130 | ### replacer
|
---|
| 131 |
|
---|
| 132 | The replacer parameter is a function `opts.replacer(key, value)` that behaves the same as the replacer
|
---|
| 133 | [from the core JSON object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Using_native_JSON#The_replacer_parameter).
|
---|
| 134 |
|
---|
| 135 | # install
|
---|
| 136 |
|
---|
| 137 | With [npm](https://npmjs.org) do:
|
---|
| 138 |
|
---|
| 139 | ```
|
---|
| 140 | npm install json-stable-stringify
|
---|
| 141 | ```
|
---|
| 142 |
|
---|
| 143 | # license
|
---|
| 144 |
|
---|
| 145 | MIT
|
---|
| 146 |
|
---|
| 147 | [package-url]: https://npmjs.org/package/json-stable-stringify
|
---|
| 148 | [npm-version-svg]: https://versionbadg.es/ljharb/json-stable-stringify.svg
|
---|
| 149 | [deps-svg]: https://david-dm.org/ljharb/json-stable-stringify.svg
|
---|
| 150 | [deps-url]: https://david-dm.org/ljharb/json-stable-stringify
|
---|
| 151 | [dev-deps-svg]: https://david-dm.org/ljharb/json-stable-stringify/dev-status.svg
|
---|
| 152 | [dev-deps-url]: https://david-dm.org/ljharb/json-stable-stringify#info=devDependencies
|
---|
| 153 | [npm-badge-png]: https://nodei.co/npm/json-stable-stringify.png?downloads=true&stars=true
|
---|
| 154 | [license-image]: https://img.shields.io/npm/l/json-stable-stringify.svg
|
---|
| 155 | [license-url]: LICENSE
|
---|
| 156 | [downloads-image]: https://img.shields.io/npm/dm/json-stable-stringify.svg
|
---|
| 157 | [downloads-url]: https://npm-stat.com/charts.html?package=json-stable-stringify
|
---|
| 158 | [codecov-image]: https://codecov.io/gh/ljharb/json-stable-stringify/branch/main/graphs/badge.svg
|
---|
| 159 | [codecov-url]: https://app.codecov.io/gh/ljharb/json-stable-stringify/
|
---|
| 160 | [actions-image]: https://img.shields.io/endpoint?url=https://github-actions-badge-u3jn4tfpocch.runkit.sh/ljharb/json-stable-stringify
|
---|
| 161 | [actions-url]: https://github.com/ljharb/json-stable-stringify/actions
|
---|