---
name: "bson_utf8_escape_for_json"
section: "3"
platform: "linux"
package: "libbson-doc"
version: "1.30.4-1+deb13u3"
release: "Debian 13 (Trixie)"
platforms: ["linux"]
url: "https://new.carta.tech/bson_utf8_escape_for_json"
---

# bson\_utf8\_escape\_for\_json(3)

## SYNOPSIS

```
char *
bson_utf8_escape_for_json (const char *utf8, ssize_t utf8_len);
```

## PARAMETERS

- •

  **utf8**: A UTF-8 encoded string.

- •

  **utf8\_len**: The length of **utf8** in bytes or -1 if it is NULL terminated.

## DESCRIPTION

Allocates a new string matching **utf8** except that special characters in JSON are escaped. The resulting string is also UTF-8 encoded.

Both " and \\ characters will be backslash-escaped. If a NUL byte is found before **utf8\_len** bytes, it is converted to "\\u0000". Other non-ASCII characters in the input are preserved.

The two-byte sequence "C0 80" is also interpreted as an internal NUL, for historical reasons. This sequence is considered invalid according to RFC3629.

## RETURNS

A newly allocated string that should be freed with *bson\_free()* when **utf8** is a valid UTF-8 string, or **NULL** if the (possibly invalid UTF-8) string could not be escaped.

## AUTHOR

MongoDB, Inc

## COPYRIGHT

2009-present, MongoDB, Inc.
