# Translate your first document

Take a CSV file and write it as JSON, YAML, and TOML, then translate one part of it, and meet the one thing a format refuses.

In this tutorial we take a small CSV file of books and write it in three other formats. Along the way we translate one row of it, name the key a format needs at its root, and see what dmog does with a document a format cannot hold. It takes about 10 minutes.

You need dmog installed. If it isn't, follow [Install dmog](https://dmog.tabnas.dev/how-to/install.html) first, then come back. The file we use is [`books.csv`](https://dmog.tabnas.dev/examples/books.csv). Download it into an empty directory and open a terminal there.

## Look at the file

Here is the file:

```console
$ cat books.csv
title,author,year
A Book of Examples,A. N. Author,1999
Notes on Formats,B. Writer,2021
```

It has a header row and two rows of books. dmog reads it as a list of two records, each keyed by the header.

## Write it as JSON

To translate a document, give `dmog translate` the format it is in, the format to write, and the file:

```console
$ dmog translate --from csv --to json books.csv
[{"title":"A Book of Examples","author":"A. N. Author","year":"1999"},{"title":"Notes on Formats","author":"B. Writer","year":"2021"}]
```

The two rows became two objects in an array. The year is a string, `"1999"`, because a CSV cell is text: CSV has no numbers, so dmog does not invent them.

## Write it as YAML

Change `--to`, and the same records come out as YAML:

```console
$ dmog translate --from csv --to yaml books.csv
- "title": "A Book of Examples"
  "author": "A. N. Author"
  "year": "1999"
- "title": "Notes on Formats"
  "author": "B. Writer"
  "year": "2021"
```

Nothing else changed. Every format is a value for `--from` and for `--to`, and the [formats reference](https://dmog.tabnas.dev/reference/formats.html) lists all 22.

## Write it as TOML

TOML is different. A TOML document is a table of keys, so its root must be an object, and our document is an array. Watch what dmog does:

```console
$ dmog translate --from csv --to toml books.csv
"items" = [ { "title" = "A Book of Examples", "author" = "A. N. Author", "year" = "1999" }, { "title" = "Notes on Formats", "author" = "B. Writer", "year" = "2021" } ]
```

dmog put the array under a key, `items`. That is the default name. We can choose a better one with `--key`:

```console
$ dmog translate --from csv --to toml --key books books.csv
"books" = [ { "title" = "A Book of Examples", "author" = "A. N. Author", "year" = "1999" }, { "title" = "Notes on Formats", "author" = "B. Writer", "year" = "2021" } ]
```

`--key` only matters for a format whose root must be an object, and only when the document's root is something else.

## Translate one part

Sometimes we want one value out of a document, not all of it. `--path` names it, as a JSON array of keys and indexes. Index `1` is the second book:

```console
$ dmog translate --from csv --to json --path '[1]' books.csv
{"title":"Notes on Formats","author":"B. Writer","year":"2021"}
```

A path can go as deep as the document does. Add the key `title`, and we get one string:

```console
$ dmog translate --from csv --to yaml --path '[1,"title"]' books.csv
"Notes on Formats"
```

The single quotes keep the shell away from the brackets and the double quotes.

## Meet a refusal

Not every format can hold every document. CSS writes style sheets, and a list of books is not a style sheet. Ask for it anyway:

```console
$ dmog translate --from csv --to css books.csv; echo $?
{"code":"TARGET_VALUE_UNREPRESENTABLE","message":"schema_only: css writes a css-ast tree, the tree its own documents read as, and this document is not one; a program that makes one can be composed with the render","output":"none"}
1
```

dmog printed nothing on standard output. On standard error it printed one JSON object: a code, a message, and `"output":"none"`, which says no output was written. The `echo $?` shows the exit status, 1. A script can rely on all three, and [Handle dmog's failures in a script](https://dmog.tabnas.dev/how-to/handle-failures.html) shows how.

## Read from standard input

Finally, the filename `-` reads standard input, so dmog fits in a pipeline:

```console
$ cat books.csv | dmog translate --from csv --to jsonl -
{"title":"A Book of Examples","author":"A. N. Author","year":"1999"}
{"title":"Notes on Formats","author":"B. Writer","year":"2021"}
```

JSON Lines writes one record to a line. That is four of the options of `translate`, and we have written one file in five formats!

## Next

The next tutorial, [Write your first program](https://dmog.tabnas.dev/tutorials/first-program.html), reshapes a document on its way through. For every option of `translate`, see the [command-line reference](https://dmog.tabnas.dev/reference/command-line.html).
