> For clean Markdown of any page, append .md to the page URL.
> For a complete documentation index, see https://rivet.chrisboulton.com/llms.txt.
> For AI client integration (Claude Code, Cursor, etc.), connect to the MCP server at https://rivet.chrisboulton.com/_mcp/server.

# Encoding functions

> Signatures, behavior, examples, and Rivet compatibility boundaries.

Encoding transforms values and text during compilation. Decode results can be traversed directly, for example `jsondecode(file("assets/config.json")).enabled`.

| Function                           | Result and rules                                                                                    |
| ---------------------------------- | --------------------------------------------------------------------------------------------------- |
| `base64decode(text)`               | Decode standard base64 to valid UTF-8 text, normalized to NFC.                                      |
| `base64encode(text)`               | NFC-normalize text and encode its UTF-8 bytes as standard base64.                                   |
| `base64gzip(text)`                 | NFC text to deterministic gzip with zero mtime, then base64; bytes can differ from Terraform.       |
| `csvdecode(text)`                  | List of string-valued maps; unique nonempty header set and consistently sized records are required. |
| `jsondecode(text)`                 | Decode JSON into scalars/lists/maps; reject duplicate keys and excessive depth/size.                |
| `jsonencode(value)`                | Compact JSON with HTML characters and Unicode line separators escaped.                              |
| `textdecodebase64(text, encoding)` | Decode base64 bytes using a supported encoding label; invalid or lossy conversion fails.            |
| `textencodebase64(text, encoding)` | NFC text to the named encoding then base64; unrepresentable characters fail.                        |
| `urlencode(text)`                  | URL form encoding; spaces become +.                                                                 |
| `yamldecode(text)`                 | One finite JSON-compatible YAML document with string keys; strict duplicate/tag/alias/depth checks. |
| `yamlencode(value)`                | Serialize to YAML; formatting can differ from Terraform.                                            |

YAML aliases are bounded; custom tags, cycles, excessive expansion, duplicate keys, and non-string keys fail. YAML `<<` has no implicit merge behavior. Character encodings use `encoding_rs` labels; UTF-16LE/BE are supported for encoding too. Use `filebase64` for arbitrary binary files, since `base64decode` returns text.

## Example

```hcl
locals {
  encodings = {
    decoded = base64decode("cml2ZXQ=")
    encoded = base64encode("rivet")
    compressed = base64gzip("rivet")
    rows = csvdecode("name,role\napp,web\n")
    config = jsondecode("{\"port\":8080}")
    json = jsonencode({ enabled = true })
    text = textdecodebase64("cml2ZXQ=", "UTF-8")
    bytes = textencodebase64("rivet", "UTF-8")
    query = urlencode("rivet docs")
    yaml = yamldecode("enabled: true\n")
    document = yamlencode({ enabled = true })
  }
}
output "encodings" {
  type  = string
  value = jsonencode(local.encodings)
}
```

All calls obey the [function limits and annotation rules](/language/functions).