# Write your first program

Write an alchemy program, check it, run it over a JSON document, then turn a nested document into a table and write the table as CSV, JSON, and YAML.

In this tutorial we write a program in alchemy, the language dmog runs, and use it to turn a nested JSON document into a flat table. We check the program, run it, render its table two ways, and use it inside a translation. It takes about 15 minutes.

You need dmog installed, and it helps to have taken [Translate your first document](https://dmog.tabnas.dev/tutorials/first-translation.html) first. Download [`records.json`](https://dmog.tabnas.dev/examples/records.json) and [`table.alc`](https://dmog.tabnas.dev/examples/table.alc) into an empty directory and open a terminal there.

## Write the smallest program

A program is a file of definitions, and dmog runs the one named `export`. Its parameter, `input`, is the document. Write this as `echo.alc`:

```alchemy
def export [input]
  json input
```

The line under `def` is indented two spaces, which makes it the body of the definition. `json input` writes the document back out as JSON.

## Check it

Before running a program, check it. `dmog check` reads the program, resolves every name in it, and builds its plan:

```console
$ dmog check echo.alc; echo $?
0
```

It printed nothing and exited 0, so the program is sound.

## Run it

Now run it over a document. `records.json` is an API response with records buried three levels down:

```console
$ dmog run echo.alc records.json
{"response":{"metadata":{"fields":[{"title":"Identifier","path":["id"]},{"title":"Full name","path":["person","name"]},{"title":"Balance","path":["account","balance"]}]},"payload":{"deep":{"records":[{"id":123,"person":{"name":"Alice"},"account":{"balance":50.25}},{"account":{"balance":72},"person":{"name":"Bob"},"id":456}]}}}}
```

The document came back unchanged, written compactly. That is what an echo does.

## Make a mistake on purpose

Change `json` to `jsn` and save the program as `typo.alc`:

```alchemy
def export [input]
  jsn input
```

Check it:

```console
$ dmog check typo.alc; echo $?
{"code":"DSL_TYPE_ERROR","message":"unknown_name: jsn is not defined","row":2,"col":3,"output":"none"}
2
```

The failure names the problem and its place: row 2, column 3, where `jsn` starts. A program that does not check exits with status 2.

## Turn the document into a table

The document carries its own description of its records. Under `response.metadata.fields`, each field has a title and a path into a record. `table.alc` uses that description to make a table:

```alchemy
def column-from-meta [source]
  record
    entry :label (get "title" source)
    entry :source
      as-path
        get "path" source

def api-binding
  record
    entry :columns
      path "response" "metadata" "fields"
    entry :rows
      path "response" "payload" "deep" "records" each-index
    entry :column column-from-meta

def export [input]
  table-from-json api-binding input
```

`api-binding` says where the columns are described, where the rows are, and how to read one column's description: its label from `title`, and the path to its cell from `path`. `export` hands the binding and the document to `table-from-json`, from alchemy's standard library.

Run it:

```console
$ dmog run table.alc records.json
"Identifier","Full name","Balance"
"123","Alice","50.25"
"456","Bob","72"
```

A table, as CSV. Bob's record lists its members in another order, and his cells still follow the columns.

## Render the table as JSON

The program returns a table and leaves the writing to dmog, which writes CSV unless told otherwise. `--render json` writes each row as an object:

```console
$ dmog run --render json table.alc records.json
[{"Identifier":123,"Full name":"Alice","Balance":50.25},{"Identifier":456,"Full name":"Bob","Balance":72}]
```

The numbers stayed numbers here, where CSV wrote them as text.

## Use the program in a translation

A program can also reshape a document on its way between formats. `translate --with` runs the program over the input, then writes what it returns in the target format:

```console
$ dmog translate --from json --to yaml --with table.alc records.json
- "Identifier": 123
  "Full name": "Alice"
  "Balance": 50.25
- "Identifier": 456
  "Full name": "Bob"
  "Balance": 72
```

The same table, now as YAML. The program does not choose the format it is written in.

## See the plan

Last, ask dmog what the program does, without running it. `dmog explain` prints the plan, and its first lines name the steps:

```console
$ dmog explain table.alc | head -4
export: table-from-json

Source reads:          1
Protocol:              JsonEvents/1 → Stream<Selected> → Stream<Value> → TableRows/1 → Text
```

The document is read once, as a stream, and the full report goes on to say what the program keeps in memory. We have a program that turns a nested response into a table and writes it three ways!

## Next

To run a program of your own over a document, see [Run a program over a document](https://dmog.tabnas.dev/how-to/run-a-program.html). The alchemy language itself is described in [the alchemy language reference](https://github.com/tabnas/alchemy/blob/main/docs/language.md), on GitHub.
