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