# canonicalize-json

> A canonical json implementation

Latest version **1.0.2** (published 2018-11-09) · MIT license · 0 weekly downloads

## Install

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

## 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.2 |
| Published | 2018-11-09 |
| First published | 2018-11-08 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 14.1 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | Mirko Kiefer |
| Maintainers | murtazasmart |

## Links

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

## Recent versions

- 1.0.2 (latest) — 2018-11-09
- 1.0.1 — 2018-11-08
- 1.0.0 — 2018-11-08

## README

# Canonical JSON

This project has been taken from https://github.com/mirkok/canonical-json and has been modified.

The goal of this module is to implement a version of JSON.stringify that returns a canonical JSON format.

Canonical JSON means that the same object should always be stringified to the exact same string.
JavaScripts native JSON.stringify does not guarantee any order for object keys when serializing:

> Properties of non-array objects are not guaranteed to be stringified in any particular order. Do not rely on ordering of properties within the same object within the stringification.

Source: [https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/JSON/stringify](https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/JSON/stringify)

This module implements two alternative solutions to this problem:

- [index.js](https://github.com/mirkok/canonical-json/blob/master/index.js) is based on [Douglas Crockford's json2.js](https://github.com/douglascrockford/JSON-js/blob/master/json2.js). It is modified to serialize object keys sorted on the fly.
- [index2.js](https://github.com/mirkok/canonical-json/blob/master/index2.js) recursively creates a copy of the object to sort its keys. The copy is then simply passed to native JSON.stringify

It currently exports the index.js version.

## Typescript
Definition file for typescript has been written only for index.js and index2.js

## Performance comparison
Performance of native JSON.stringify and the two alternative implementations that output keys sorted:

- native JSON.stringify: `75 ms`
- js JSON.stringify with sorted keys ([implementation](https://github.com/mirkok/canonical-json/blob/master/index.js)): `308 ms`
- copy and native JSON.stringify with sorted keys ([implementation](https://github.com/mirkok/canonical-json/blob/master/index2.js)): `291 ms`

The tests were run in Node.js on a 2011 MacBook Pro.
Performance test source: [test/performance.js](https://github.com/mirkok/canonical-json/blob/master/test/performance.js)

## Command Line Interface

To run this from the command line you can use `canonical-json.js` like so:

```
cat sample.json | ./canonical-json.js > sample-canonical.json
```

## Links
- [CANON](https://github.com/davidchambers/CANON) is a project with similar goals.

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