# Translate a document to another format

Write a file, or standard input, in another format, and save the result.

You have a document in one format and need it in another. `dmog translate` takes the source format, the target format, and the document.

## Translate a file

Name both formats and the file:

```console
$ dmog translate --from yaml --to json server.yaml
{"server":{"host":"example.com","port":8080,"tls":true},"paths":["/api","/health"]}
```

The identifiers `--from` and `--to` take are listed in the [formats reference](https://dmog.tabnas.dev/reference/formats.html). dmog never guesses a format from a file's name.

## Translate standard input

Give `-` as the file to read standard input:

```console
$ cat server.yaml | dmog translate --from yaml --to json5 -
{"server":{"host":"example.com","port":8080,"tls":true},"paths":["/api","/health"]}
```

## Save the result

dmog writes the translation to standard output and nothing else, so a redirect saves it:

```console
$ dmog translate --from yaml --to toml server.yaml > server.toml
$ cat server.toml
"server" = { "host" = "example.com", "port" = 8080, "tls" = true }
"paths" = [ "/api", "/health" ]
```

A failure goes to standard error, so it never ends up in the saved file. The file can still hold part of a translation that failed part way, which [Handle dmog's failures in a script](https://dmog.tabnas.dev/how-to/handle-failures.html) covers.

## What to watch for

A format keeps what it can say. YAML written as JSON loses its comments, and CSV written as anything keeps its cells as strings. Each format's section of the [formats reference](https://dmog.tabnas.dev/reference/formats.html) lists what a document written in it does not keep.

Some formats write only their own kind of document. C, CSS, PGN, Protocol Buffers, and the grammar notations refuse another format's tree with `TARGET_VALUE_UNREPRESENTABLE`. For why, see [What a translation keeps](https://dmog.tabnas.dev/explanation/translation.html).
