# JSON canonical serialize

`json-canonical-serialize` · version 1.0.0 · JSON & JSONL · free, no key needed

Serialize a JSON value to deterministic compact text with sorted object keys and safe integers only.

**Use when you need to: json canonical serialize · canonicalize json · serialize json deterministically.**

## Supported

- json canonical serialize
- canonicalize json
- serialize json deterministically

## Not supported

- serialize non-integer numbers
- pretty-print json
- jsonc comments
- locale-aware key sort
- read files
- fetch urls

## Behavior

- The output is compact JSON with no insignificant whitespace.
- Object keys are sorted by UTF-16 code units and serialized with JSON.stringify escaping.
- Array element order is preserved.
- Strings use JSON.stringify escaping.
- null, true, and false are emitted as those literals.
- Numbers must be safe integers and are serialized with String(n), never exponent notation.
- Only own enumerable string keys of objects are serialized; nothing is skipped.
- Object.create(null) inputs are accepted; prototype-like own keys are ordinary keys.
- The canonical UTF-8 byte length must not exceed 1048576.

## Input

- `value` (any JSON value, required)

## Output

- `json` (string, required): max length 1048576

## Limits

- max output bytes: 1048576

## Example

Request input:

```json
{
  "value": {
    "z": 1,
    "note": "hello \"Ada\"",
    "a": [
      true,
      false,
      null,
      0
    ],
    "m": {
      "b": "x",
      "a": "y"
    }
  }
}
```

Response:

```json
{
  "result": {
    "json": "{\"a\":[true,false,null,0],\"m\":{\"a\":\"y\",\"b\":\"x\"},\"note\":\"hello \\\"Ada\\\"\",\"z\":1}"
  }
}
```

## How to call it

### MCP

Connect `https://computefirst.net/mcp` ([setup](/docs#connect)), then call `execute` with:

```json
{
  "id": "json-canonical-serialize",
  "version": "1.0.0",
  "input": {
    "value": {
      "z": 1,
      "note": "hello \"Ada\"",
      "a": [
        true,
        false,
        null,
        0
      ],
      "m": {
        "b": "x",
        "a": "y"
      }
    }
  }
}
```

### HTTP (no key)

```sh
curl -X POST https://computefirst.net/v1/tools/json-canonical-serialize/versions/1.0.0/execute \
  -H "Content-Type: application/json" \
  -d '{"value":{"z":1,"note":"hello \"Ada\"","a":[true,false,null,0],"m":{"b":"x","a":"y"}}}'
```

The machine-readable contract is at [/v1/tools/json-canonical-serialize/versions/1.0.0](/v1/tools/json-canonical-serialize/versions/1.0.0).

### CLI

```sh
node cli.mjs run json-canonical-serialize 1.0.0 --input input.json --base-url https://computefirst.net
```

Get the client at [/clients/cli/](/clients/cli/).

## Related tools

- [Array to JSONL](/tools/array-to-jsonl): Serialize an array of JSON-safe values as JSON Lines text.
- [JSON to CSV](/tools/json-to-csv): Serialize string-valued JSON rows to CSV using explicit ordered columns.
- [JSON array anti join](/tools/json-array-anti-join): Anti join two JSON arrays of objects on exact keys.
- [JSON array concat](/tools/json-array-concat): Concatenate multiple JSON arrays of objects into one.
- [JSON array count](/tools/json-array-count): Count structural occurrences in a JSON array in first-seen order.
- [JSON array distinct by](/tools/json-array-distinct-by): Deduplicate a JSON array of objects based on specific keys.
