# File formats

SP CMS picks a format from the file extension, so you rarely need to set one. Set `format` on a collection or file only to override the guess.

## Inferred formats

| Extension                                            | With `fields`      | Without `fields`                       |
| ---------------------------------------------------- | ------------------ | -------------------------------------- |
| `.json`                                              | `json`             | `code`                                 |
| `.yml`, `.yaml`                                      | `yaml`             | `code`                                 |
| `.toml`                                              | `toml`             | `raw`                                  |
| `.md`, `.mdx`, `.html`, `.js`, `.jsx`, `.ts`, `.tsx` | `yaml-frontmatter` | `code`                                 |
| `.csv`                                               | —                  | `datagrid`, shown in the source editor |
| Anything else                                        | `yaml-frontmatter` | `raw`                                  |

A collection's extension comes from its [`filename`](/configuration/content/filename/) template; a file's comes from its `path`.

## All formats

| `format`               | Reads and writes                              |
| ---------------------- | --------------------------------------------- |
| `yaml-frontmatter`     | Front matter between `---` lines, plus a body |
| `json-frontmatter`     | JSON front matter, plus a body                |
| `toml-frontmatter`     | Front matter between `+++` lines, plus a body |
| `yaml`, `json`, `toml` | A whole data file                             |
| `code`                 | Source editor with syntax highlighting        |
| `raw`                  | Plain text editor                             |
| `datagrid`             | CSV, shown in the source editor               |

## Front matter and the body

In front matter formats, each field maps to a front matter key, except a field named `body`, which maps to everything below the front matter:

```yaml
fields:
  - name: title
    type: string
  - name: body
    type: rich-text
```

For non-standard delimiters, set `delimiters`, for example `delimiters: "+++"` or `delimiters: ["<!--", "-->"]`.

If SP CMS can't read a file in a collection, the file stays in the list but some fields may be missing. A warning names the affected files; after fixing them, choose **Refresh** to check them again.

## Source editing

Content with `fields` is always edited and saved through its form, so the rules in `.pages.yml` are checked on every save. Only content without `fields` opens as source. That's useful for `robots.txt`, redirect rules or snippets:

```yaml
- name: redirects
  label: Redirects
  type: file
  path: public/_redirects
  format: code
```

Files with known code extensions open in the code editor automatically. Set `format: code` for files without an extension, such as `_redirects`.

## Keeping unknown keys

By default, saving a structured file writes only the keys your fields describe. If your fields cover only part of a file, set [`settings.content.merge: true`](/configuration/settings/#content-merging) to keep the rest.
