> **Can't find what you're looking for?** Use `search_docs` on the docs MCP server at `https://v3.viem.sh/api/mcp` to find what you need.

# Json.canonicalize

:::info
`Json.canonicalize` is a re-export of ox's [`Json.canonicalize`](https://oxlib.sh/api/Json/canonicalize). Refer to the ox documentation for the full reference.
:::

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

```ts
import { Json } from 'viem/utils'
```

## Examples

```ts twoslash
// @noErrors
import { Json } from 'viem/utils'

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

```ts twoslash
// @noErrors
import { Json } from 'viem/utils'

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
```

## Parameters

### value

* **Type:** `unknown`

The value to canonicalize.

## Return Type

The canonical JSON string.

`string`
