{"id":15650671,"url":"https://github.com/kikobeats/json-stringify-deterministic","last_synced_at":"2025-07-12T03:07:50.636Z","repository":{"id":10896932,"uuid":"67401257","full_name":"Kikobeats/json-stringify-deterministic","owner":"Kikobeats","description":"Deterministic version of JSON.stringify() so you can get a consistent hash from stringified results.","archived":false,"fork":false,"pushed_at":"2024-12-25T17:41:57.000Z","size":69,"stargazers_count":38,"open_issues_count":0,"forks_count":8,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-03-28T18:07:12.227Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Kikobeats.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2016-09-05T08:13:16.000Z","updated_at":"2025-03-12T08:58:43.000Z","dependencies_parsed_at":"2023-01-13T16:13:00.850Z","dependency_job_id":"e1f3652e-26ff-475f-b4c0-f5fbcaa3fa1d","html_url":"https://github.com/Kikobeats/json-stringify-deterministic","commit_stats":{"total_commits":51,"total_committers":7,"mean_commits":7.285714285714286,"dds":0.196078431372549,"last_synced_commit":"16dd2ec306ff0f18dda16d5180e7aa900794e1c5"},"previous_names":[],"tags_count":13,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Kikobeats%2Fjson-stringify-deterministic","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Kikobeats%2Fjson-stringify-deterministic/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Kikobeats%2Fjson-stringify-deterministic/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Kikobeats%2Fjson-stringify-deterministic/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Kikobeats","download_url":"https://codeload.github.com/Kikobeats/json-stringify-deterministic/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247234922,"owners_count":20905854,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-10-03T12:35:27.698Z","updated_at":"2025-04-04T19:05:02.153Z","avatar_url":"https://github.com/Kikobeats.png","language":"JavaScript","readme":"# json-stringify-deterministic\n\n![Last version](https://img.shields.io/github/tag/Kikobeats/json-stringify-deterministic.svg?style=flat-square)\n[![Coverage Status](https://img.shields.io/coveralls/Kikobeats/json-stringify-deterministic.svg?style=flat-square)](https://coveralls.io/github/Kikobeats/json-stringify-deterministic)\n[![NPM Status](https://img.shields.io/npm/dm/json-stringify-deterministic.svg?style=flat-square)](https://www.npmjs.org/package/json-stringify-deterministic)\n\n\u003e Deterministic version of `JSON.stringify()`, so you can get a consistent hash from stringified results.\n\nSimilar to [json-stable-stringify](https://github.com/substack/json-stable-stringify) *but*:\n\n- No Dependencies. Minimal as possible.\n- Better cycles detection.\n- Support serialization for object without `.toJSON` (such as `RegExp`).\n- Provides built-in TypeScript declarations.\n\n## Install\n\n```bash\nnpm install json-stringify-deterministic --save\n```\n\n## Usage\n\n```js\nconst stringify = require('json-stringify-deterministic')\nconst obj = { c: 8, b: [{ z: 6, y: 5, x: 4 }, 7], a: 3 }\n\nconsole.log(stringify(obj))\n// =\u003e {\"a\":3,\"b\":[{\"x\":4,\"y\":5,\"z\":6},7],\"c\":8}\n```\n\n## API\n\n### stringify(\u0026lt;obj\u0026gt;, [opts])\n\n#### obj\n\n*Required*\u003cbr\u003e\nType: `object`\n\nThe input `object` to be serialized.\n\n#### opts\n\n##### opts.stringify\n\nType: `function`\nDefault: `JSON.stringify`\n\nDeterminate how to stringify primitives values.\n\n##### opts.cycles\n\nType: `boolean`\nDefault: `false`\n\nDeterminate how to resolve cycles.\n\nUnder `true`, when a cycle is detected, `[Circular]` will be inserted in the node.\n\n##### opts.compare\n\nType: `function`\n\nCustom comparison function for object keys.\n\nYour function `opts.compare` is called with these parameters:\n\n``` js\nopts.cmp({ key: akey, value: avalue }, { key: bkey, value: bvalue })\n```\n\nFor example, to sort on the object key names in reverse order you could write:\n\n``` js\nconst stringify = require('json-stringify-deterministic')\n\nconst obj = { c: 8, b: [{z: 6,y: 5,x: 4}, 7], a: 3 }\nconst objSerializer = stringify(obj, function (a, b) {\n  return a.key \u003c b.key ? 1 : -1\n})\n\nconsole.log(objSerializer)\n// =\u003e {\"c\":8,\"b\":[{\"z\":6,\"y\":5,\"x\":4},7],\"a\":3}\n```\n\nOr if you wanted to sort on the object values in reverse order, you could write:\n\n```js\nconst stringify = require('json-stringify-deterministic')\n\nconst obj = { d: 6, c: 5, b: [{ z: 3, y: 2, x: 1 }, 9], a: 10 }\nconst objtSerializer = stringify(obj, function (a, b) {\n  return a.value \u003c b.value ? 1 : -1\n})\n\nconsole.log(objtSerializer)\n// =\u003e {\"d\":6,\"c\":5,\"b\":[{\"z\":3,\"y\":2,\"x\":1},9],\"a\":10}\n```\n\n##### opts.space\n\nType: `string`\u003cbr\u003e\nDefault: `''`\n\nIf you specify `opts.space`, it will indent the output for pretty-printing.\n\nValid values are strings (e.g. `{space: \\t}`). For example:\n\n```js\nconst stringify = require('json-stringify-deterministic')\n\nconst obj = { b: 1, a: { foo: 'bar', and: [1, 2, 3] } }\nconst objSerializer = stringify(obj, { space: '  ' })\nconsole.log(objSerializer)\n// =\u003e {\n//   \"a\": {\n//     \"and\": [\n//       1,\n//       2,\n//       3\n//     ],\n//     \"foo\": \"bar\"\n//   },\n//   \"b\": 1\n// }\n```\n\n##### opts.replacer\n\nType: `function`\u003cbr\u003e\n\nThe replacer parameter is a function `opts.replacer(key, value)` that behaves\nthe same as the replacer\n[from the core JSON object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Using_native_JSON#The_replacer_parameter).\n\n## Related\n\n- [sort-keys-recursive](https://github.com/Kikobeats/sort-keys-recursive): Sort the keys of an array/object recursively.\n\n## License\n\nMIT © [Kiko Beats](https://github.com/Kikobeats).\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkikobeats%2Fjson-stringify-deterministic","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fkikobeats%2Fjson-stringify-deterministic","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkikobeats%2Fjson-stringify-deterministic/lists"}