# Json.canonicalize

Serializes a value to a canonical JSON string as defined by [RFC 8785 (JSON Canonicalization Scheme)](https://www.rfc-editor.org/rfc/rfc8785).

* Object keys are sorted recursively by UTF-16 code unit comparison. - Primitives are serialized per ECMAScript rules (no trailing zeros on numbers, etc.). - No whitespace is inserted.

## Imports

:::code-group
```ts [Named]
import { Json } from 'ox'
```

```ts [Entrypoint]
import * as Json from 'ox/Json'
```
:::

## Examples

```ts twoslash
import { Json } from 'ox'

const json = Json.canonicalize({ b: 2, a: 1 })
// @log: '{"a":1,"b":2}'
```

```ts twoslash
import { Json } from 'ox'

const json = Json.canonicalize({
  z: [3, { y: 1, x: 2 }],
  a: 'hello'
})
// @log: '{"a":"hello","z":[3,{"x":2,"y":1}]}'
```

## Definition

```ts
function canonicalize(
  value: unknown,
): string
```

**Source:** [src/core/Json.ts](https://github.com/wevm/ox/blob/main/src/core/Json.ts#L161)

## Parameters

### value

* **Type:** `unknown`

The value to canonicalize.

## Return Type

The canonical JSON string.

`string`
