Installations
npm install json-stringify-deterministic
Developer Guide
Typescript
Yes
Module System
CommonJS
Min. Node Version
>= 4
Node Version
18.18.2
NPM Version
9.8.1
Score
99.5
Supply Chain
100
Quality
75.9
Maintenance
100
Vulnerability
100
License
Releases
Contributors
Unable to fetch Contributors
Languages
JavaScript (100%)
Developer
kikobeats
Download Statistics
Total Downloads
9,542,290
Last Day
18,553
Last Week
75,906
Last Month
347,196
Last Year
3,545,350
GitHub Statistics
37 Stars
73 Commits
8 Forks
3 Watching
1 Branches
6 Contributors
Bundle Size
1.49 kB
Minified
769.00 B
Minified + Gzipped
Package Meta Information
Latest Version
1.0.12
Package Id
json-stringify-deterministic@1.0.12
Unpacked Size
11.36 kB
Size
4.45 kB
File Count
7
NPM Version
9.8.1
Node Version
18.18.2
Publised On
24 Oct 2023
Total Downloads
Cumulative downloads
Total Downloads
9,542,290
Last day
0.2%
18,553
Compared to previous day
Last week
-18.1%
75,906
Compared to previous week
Last month
1.4%
347,196
Compared to previous month
Last year
60%
3,545,350
Compared to previous year
Daily Downloads
Weekly Downloads
Monthly Downloads
Yearly Downloads
json-stringify-deterministic
Deterministic version of
JSON.stringify()
, so you can get a consistent hash from stringified results.
Similar to json-stable-stringify but:
- No Dependencies. Minimal as possible.
- Better cycles detection.
- Support serialization for object without
.toJSON
(such asRegExp
). - Provides built-in TypeScript declarations.
Install
1npm install json-stringify-deterministic --save
Usage
1const stringify = require('json-stringify-deterministic') 2const obj = { c: 8, b: [{ z: 6, y: 5, x: 4 }, 7], a: 3 } 3 4console.log(stringify(obj)) 5// => {"a":3,"b":[{"x":4,"y":5,"z":6},7],"c":8}
API
stringify(<obj>, [opts])
obj
Required
Type: object
The input object
to be serialized.
opts
opts.stringify
Type: function
Default: JSON.stringify
Determinate how to stringify primitives values.
opts.cycles
Type: boolean
Default: false
Determinate how to resolve cycles.
Under true
, when a cycle is detected, [Circular]
will be inserted in the node.
opts.compare
Type: function
Custom comparison function for object keys.
Your function opts.compare
is called with these parameters:
1opts.cmp({ key: akey, value: avalue }, { key: bkey, value: bvalue })
For example, to sort on the object key names in reverse order you could write:
1const stringify = require('json-stringify-deterministic') 2 3const obj = { c: 8, b: [{z: 6,y: 5,x: 4}, 7], a: 3 } 4const objSerializer = stringify(obj, function (a, b) { 5 return a.key < b.key ? 1 : -1 6}) 7 8console.log(objSerializer) 9// => {"c":8,"b":[{"z":6,"y":5,"x":4},7],"a":3}
Or if you wanted to sort on the object values in reverse order, you could write:
1const stringify = require('json-stringify-deterministic') 2 3const obj = { d: 6, c: 5, b: [{ z: 3, y: 2, x: 1 }, 9], a: 10 } 4const objtSerializer = stringify(obj, function (a, b) { 5 return a.value < b.value ? 1 : -1 6}) 7 8console.log(objtSerializer) 9// => {"d":6,"c":5,"b":[{"z":3,"y":2,"x":1},9],"a":10}
opts.space
Type: string
Default: ''
If you specify opts.space
, it will indent the output for pretty-printing.
Valid values are strings (e.g. {space: \t}
). For example:
1const stringify = require('json-stringify-deterministic') 2 3const obj = { b: 1, a: { foo: 'bar', and: [1, 2, 3] } } 4const objSerializer = stringify(obj, { space: ' ' }) 5console.log(objSerializer) 6// => { 7// "a": { 8// "and": [ 9// 1, 10// 2, 11// 3 12// ], 13// "foo": "bar" 14// }, 15// "b": 1 16// }
opts.replacer
Type: function
The replacer parameter is a function opts.replacer(key, value)
that behaves
the same as the replacer
from the core JSON object.
Related
- sort-keys-recursive: Sort the keys of an array/object recursively.
License
MIT © Kiko Beats.
No vulnerabilities found.
Reason
no vulnerabilities detected
Reason
update tool detected
Details
- Info: Dependabot detected: .github/dependabot.yml:1
Reason
no binaries found in the repo
Reason
no dangerous workflow patterns detected
Reason
license file detected
Details
- Info: : LICENSE.md:1
Reason
dependency not pinned by hash detected -- score normalized to 7
Details
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/main.yml:16: update your workflow using https://app.stepsecurity.io/secureworkflow/bninja/sqlalchemy-pilo/main.yml/master?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/main.yml:18: update your workflow using https://app.stepsecurity.io/secureworkflow/bninja/sqlalchemy-pilo/main.yml/master?enable=pin
- Warn: third-party GitHubAction not pinned by hash: .github/workflows/main.yml:28: update your workflow using https://app.stepsecurity.io/secureworkflow/bninja/sqlalchemy-pilo/main.yml/master?enable=pin
- Warn: npmCommand not pinned by hash: .github/workflows/main.yml:23
- Info: Dockerfile dependencies are pinned
- Info: no insecure (not pinned by hash) dependency downloads found in Dockerfiles
- Info: no insecure (not pinned by hash) dependency downloads found in shell scripts
Reason
GitHub code reviews found for 16 commits out of the last 30 -- score normalized to 5
Details
- Warn: no reviews found for commit: 760fb33d1d5c2d0d8e9011258f47cb2bec8b2ab5
- Warn: no reviews found for commit: deb51ab1c184737ae3384c1adbde1bd1016977ae
- Warn: no reviews found for commit: 804892529368de4e4b289fac1db177fcf8a55c72
- Warn: no reviews found for commit: 5765f465d779e3c795e78835e4c5633b8ce45ff7
- Warn: no reviews found for commit: 9830440ece1092fc581e0940e4a761b68d4f6c5e
- Warn: no reviews found for commit: b62b0ea1c0658c976c575d19b233a40e59a92691
- Warn: no reviews found for commit: 4234c7f5c4072f17d47ceb15ad53fa7e6c4848a1
- Warn: no reviews found for commit: dba404dfb358b17b11280fc47963a1598781815a
- Warn: no reviews found for commit: 4ce3ea748d355ed6dd3d3002eb8e09f1eeb32986
- Warn: no reviews found for commit: bc88f1e1dd2e1bcade09ff5c8bdaa155b04d41cc
- Warn: no reviews found for commit: 69f66827b448dc7241d90dae3cfc1f2aa34a2b9b
- Warn: no reviews found for commit: 6aaec5dfab0626a21c213ed3b9f7dc028d6bdc63
- Warn: no reviews found for commit: 531aea7c56569f9532635d283a9c9314e1457ee1
- Warn: no reviews found for commit: cbebb9a0ab724275e09454038ced8ea16ba45a78
Reason
0 commit(s) out of 30 and 0 issue activity out of 2 found in the last 90 days -- score normalized to 0
Reason
no badge detected
Reason
branch protection not enabled on development/release branches
Details
- Warn: branch protection not enabled for branch 'master'
Reason
non read-only tokens detected in GitHub workflows
Details
- Warn: no topLevel permission defined: .github/workflows/main.yml:1: update your workflow using https://app.stepsecurity.io/secureworkflow/bninja/sqlalchemy-pilo/main.yml/master?enable=permissions
Reason
security policy file not detected
Reason
project is not fuzzed
Score
5.1
/10
Last Scanned on 2022-08-15
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn MoreOther packages similar to json-stringify-deterministic
fast-json-stable-stringify
deterministic `JSON.stringify()` - a faster version of substack's json-stable-strigify without jsonify
json-stable-stringify-without-jsonify
deterministic JSON.stringify() with custom sorting to get deterministic hashes from stringified results, with no public domain dependencies
json-stable-stringify
deterministic JSON.stringify() with custom sorting to get deterministic hashes from stringified results
safe-stable-stringify
Deterministic and safely JSON.stringify to quickly serialize JavaScript objects