# Fairspec File Dialect

<table>
  <tr>
    <th>Authors</th>
    <td>Evgeny Karev</td>
  </tr>
  <tr>
    <th>Profile</th>
    <td>
      <a href="https://fairspec.org/profiles/latest/file-dialect.json">https://fairspec.org/profiles/latest/file-dialect.json</a>
    </td>
  </tr>
</table>

Fairspec File Dialect is a simple [JSON](https://json.org)-based format that defines File Dialect to describe a file's format options and features. When expedient, this specification aims to be structurally compatible with the [Data Package](https://datapackage.org/standard/data-package/) standard.

## Language

The key words `MUST`, `MUST NOT`, `REQUIRED`, `SHALL`, `SHALL NOT`, `SHOULD`, `SHOULD NOT`, `RECOMMENDED`, `MAY`, and `OPTIONAL` in this document are to be interpreted as described in [RFC 2119](https://www.ietf.org/rfc/rfc2119.txt).

## Descriptor

A Fairspec File Dialect is a [JSON](https://json.org/) resource that `MUST` be an object compatible with the [File Dialect](#file-dialect) structure outlined below.

## File Dialect

A top-level descriptor object describing a file dialect. It `MUST` have a [`format`](#format) and `MAY` have the following properties (all optional unless otherwise stated):

### `$schema` [#profile]

> [!NOTE]
> Data publishers `SHOULD` provide this property with an exact version when sharing a standalone descriptor publicly.

[External Path](#external-path) to one of the officially published Fairspec File Dialect profiles with default value `https://fairspec.org/profiles/latest/file-dialect.json`.

For example for version X.Y.Z of the profile:

```json
{
  "$schema": "https://fairspec.org/profiles/X.Y.Z/file-dialect.json",
  "format": "csv"
}
```

### `format` [required] [#format]

The file format of the dialect. It is required and `MUST` be a `string`.

For example for a CSV file:

```json
{
  "fileDialect": {
    "format": "csv"
  }
}
```

### `title`

An optional human-readable title for the format.

For example:

```json
{
  "fileDialect": {
    "format": "custom",
    "title": "My custom format"
  }
}
```

### `description`

An optional detailed description of the format.

For example:

```json
{
  "fileDialect": {
    "format": "custom",
    "title": "My custom format",
    "description": "You can open this file with OpenOffice"
  }
}
```

### File Formats

A UTF-8 byte order mark at the start of a CSV, TSV, JSON or JSONL file is not part of its content: an implementation `MUST` ignore it.

#### CSV

A format for comma-separated values files. It `MUST` have [`format`](#format) set to `"csv"`. It `MUST` be `utf-8` encoded. An unquoted empty cell (`,,`) is `null`, and a quoted empty cell (`,"",`) is an empty string.

A line with no characters at all is blank: it is not a row and takes no row number, wherever it is in the file. The terminator of the last line ends that row and starts no other, so a file ending in a terminator has no empty last row. A line holding only delimiters, such as `,,`, is a row whose cells are all `null`.

A row with fewer cells than the table has columns is read with `null` in the cells it lacks. A row with more cells than the table has columns cannot be read: loading the file `MUST` fail, and validating it `MUST` report that the file cannot be loaded.

The names a header gives the columns `MUST` be unique; a header that repeats a name is an error. An empty name is a valid name, so two empty names are a repeated one.

Metadata example:

```json
{
  "fileDialect": {
    "format": "csv"
  }
}
```

Data example:

```csv
name,age,city
Alice,30,New York
Bob,25,London
Charlie,35,Tokyo
```

Format properties:

- [`delimiter`](#delimiter)
- [`lineTerminator`](#lineterminator)
- [`quoteChar`](#quotechar)
- [`nullSequence`](#nullsequence)
- [`headerRows`](#headerrows)
- [`headerJoin`](#headerjoin)
- [`commentRows`](#commentrows)
- [`commentPrefix`](#commentprefix)
- [`columnNames`](#columnnames)

#### TSV

A format for tab-separated values files. It `MUST` have [`format`](#format) set to `"tsv"`. It `MUST` be `utf-8` encoded. An empty cell (two adjacent tabs) is `null`.

TSV has no quoting: a `"` is a literal character of its cell, and a cell cannot hold a tab or a line terminator. Blank lines, short and long rows and the names a header gives follow the rules of [CSV](#csv).

Metadata example:

```json
{
  "fileDialect": {
    "format": "tsv",
    "nullSequence": "\\N"
  }
}
```

Data example:

```tsv
name	age	city
Alice	30	New York
Bob	25	London
Charlie	35	Tokyo
```

Format properties:

- [`lineTerminator`](#lineterminator)
- [`nullSequence`](#nullsequence)
- [`headerRows`](#headerrows)
- [`headerJoin`](#headerjoin)
- [`commentRows`](#commentrows)
- [`commentPrefix`](#commentprefix)
- [`columnNames`](#columnnames)

#### JSON

A format for JSON array files. It `MUST` have [`format`](#format) set to `"json"`. The rows are the items of the array at [`jsonPointer`](#jsonpointer), or of the whole document without it. A JSON file whose top-level value is an object, read without a `jsonPointer`, is not a table.

Metadata example:

```json
{
  "fileDialect": {
    "format": "json",
    "jsonPointer": "/data/items"
  }
}
```

Data example:

```json
[
  { "name": "Alice", "age": 30, "city": "New York" },
  { "name": "Bob", "age": 25, "city": "London" },
  { "name": "Charlie", "age": 35, "city": "Tokyo" }
]
```

Format properties:

- [`headerRows`](#headerrows)
- [`headerJoin`](#headerjoin)
- [`commentRows`](#commentrows)
- [`commentPrefix`](#commentprefix)
- [`columnNames`](#columnnames)
- [`jsonPointer`](#jsonpointer)
- [`rowType`](#rowtype)

#### JSONL

A format for JSON Lines files (newline-delimited JSON). It `MUST` have [`format`](#format) set to `"jsonl"`.

As in [CSV](#csv), a line with no characters at all is blank: it is not a row and takes no row number, and the terminator of the last line starts no other row. A line holding only JSON whitespace (spaces, tabs and carriage returns) is blank too: JSON allows whitespace around a value, and such a line holds none.

Metadata example:

```json
{
  "fileDialect": {
    "format": "jsonl",
    "rowType": "object"
  }
}
```

Data example:

```jsonl
{"name": "Alice", "age": 30, "city": "New York"}
{"name": "Bob", "age": 25, "city": "London"}
{"name": "Charlie", "age": 35, "city": "Tokyo"}
```

Format properties:

- [`headerRows`](#headerrows)
- [`headerJoin`](#headerjoin)
- [`commentRows`](#commentrows)
- [`commentPrefix`](#commentprefix)
- [`columnNames`](#columnnames)
- [`rowType`](#rowtype)

#### XLSX

A format for Microsoft Excel files. It `MUST` have [`format`](#format) set to `"xlsx"`. Empty cells are `null` values. A merged range holds its value in its top-left cell only; its other cells are empty.

Metadata example:

```json
{
  "fileDialect": {
    "format": "xlsx",
    "sheetNumber": 2
  }
}
```

Data example:

```
<binary data>
```

Format properties:

- [`headerRows`](#headerrows)
- [`headerJoin`](#headerjoin)
- [`commentRows`](#commentrows)
- [`commentPrefix`](#commentprefix)
- [`columnNames`](#columnnames)
- [`sheetName`](#sheetname)
- [`sheetNumber`](#sheetnumber)

#### ODS

A format for OpenDocument Spreadsheet files. It `MUST` have [`format`](#format) set to `"ods"`. Empty cells are `null` values. A merged range holds its value in its top-left cell only; its other cells are empty.

Metadata example:

```json
{
  "fileDialect": {
    "format": "ods",
    "sheetName": "Data Sheet"
  }
}
```

Data example:

```
<binary data>
```

Format properties:

- [`headerRows`](#headerrows)
- [`headerJoin`](#headerjoin)
- [`commentRows`](#commentrows)
- [`commentPrefix`](#commentprefix)
- [`columnNames`](#columnnames)
- [`sheetName`](#sheetname)
- [`sheetNumber`](#sheetnumber)

#### Parquet

A format for Apache Parquet files. It `MUST` have [`format`](#format) set to `"parquet"`.

Metadata example:

```json
{
  "fileDialect": {
    "format": "parquet"
  }
}
```

Data example:

```
<binary data>
```

#### Arrow

A format for Apache Arrow files. It `MUST` have [`format`](#format) set to `"arrow"`.

Metadata example:

```json
{
  "fileDialect": {
    "format": "arrow"
  }
}
```

Data example:

```
<binary data>
```

#### SQLite

A format for SQLite database files. It `MUST` have [`format`](#format) set to `"sqlite"`.

Metadata example:

```json
{
  "fileDialect": {
    "format": "sqlite"
  }
}
```

Data example:

```
<binary data>
```

Format properties:

- [`tableName`](#tablename)

#### Custom

A format for custom data. It `MUST` have [`format`](#format) not supported by the formats above.

Metadata example:

```json
{
  "fileDialect": {
    "format": "custom",
    "title": "Custom format",
    "description": "Custom format description"
  }
}
```

Data example:

```
<binary data>
```

### Format Properties

A **row** is one parsed record of the file: in CSV and TSV, a quoted cell spanning several lines belongs to one row. Rows are numbered from `1`, counting every row of the file before any is removed, so [`headerRows`](#headerrows) and [`commentRows`](#commentrows) refer to the same numbering. A blank line of CSV, TSV or JSONL is not a row and is not counted. A row `MUST NOT` be listed both in `headerRows` and in `commentRows`: a dialect that lists one in both is invalid.

A property the dialect leaves out takes its default, as stated for each property below; an implementation `MUST NOT` infer it from the data. Inference is only for a resource without a dialect (see [Dataset](../dataset#filedialect)), so a declared dialect reads the same in every implementation.

A file is never read with a dialect that is not valid: loading the file `MUST` fail, and validating it `MUST` report a metadata error at the dialect property that is not valid instead of checking the data.

#### `delimiter`

> [!NOTE]
> Supported formats: **csv**

It `MUST` be a string of one character length. This property specifies the character sequence which separates fields in the data file. It defaults to a comma (`,`); TSV always separates fields with a tab.

For example:

```json
{
  "fileDialect": {
    "format": "csv",
    "delimiter": ";"
  }
}
```

For a file like:

```
id;name;price
1;apple;1.50
2;orange;2.00
```

#### `lineTerminator`

> [!NOTE]
> Supported formats: **csv**, **tsv**

It `MUST` be a string. This property specifies the character sequence which terminates rows in the file. Common values are `\n` (Unix), `\r\n` (Windows), `\r` (old Mac). It defaults to `\n`, and a `\r` directly before the terminator is not part of the row, so `\r\n` files read the same under the default.

For example:

```json
{
  "fileDialect": {
    "format": "csv",
    "lineTerminator": "\r\n"
  }
}
```

#### `quoteChar`

> [!NOTE]
> Supported formats: **csv**

It `MUST` be a string of one character length. This property specifies a character to use for quoting in case the delimiter needs to be used inside a data cell. It defaults to a double quote (`"`).

For example:

```json
{
  "fileDialect": {
    "format": "csv",
    "quoteChar": "'"
  }
}
```

For a file like:

```
id,name
1,'apple,red'
2,'orange,citrus'
```

#### `nullSequence`

> [!NOTE]
> Supported formats: **csv**, **tsv**

It `MUST` be a string. This property specifies the null sequence representing missing values in the data. An unquoted cell equal to the sequence is read as `null`, in addition to the empty cells the format reads as `null`. A quoted cell is a string even when its text equals the sequence, as a quoted empty cell is.

For example:

```json
{
  "fileDialect": {
    "format": "csv",
    "nullSequence": "\\N"
  }
}
```

For a file like:

```
id,name,notes
1,apple,fresh
2,orange,\N
3,banana,\N
```

#### `headerRows`

> [!NOTE]
> Supported formats: **csv**, **tsv**, **json**, **jsonl**, **xlsx**, **ods**

It `MUST` be `false` or an array of positive integers starting from `1`. This property specifies the row numbers for the header. It defaults to `[1]`, the first row; `false` means the file has no header, and so does an empty array `[]`.

The listed rows form the header, joined top to bottom, so the array `MUST` list them in ascending order: a dialect with `headerRows` such as `[3, 1]` is invalid. Every row up to the last header row is consumed by the header and is never data: a row between two header rows that the list leaves out is dropped. With `[1, 3]`, rows `1` and `3` form the header, row `2` is dropped and the data starts at row `4`. A header row past the end of the file is an error.

For example with a single header row:

```json
{
  "fileDialect": {
    "format": "csv",
    "headerRows": [1]
  }
}
```

For example with multi-line headers:

```json
{
  "fileDialect": {
    "format": "csv",
    "headerRows": [1, 2]
  }
}
```

For a file like:

```
fruit
id,name,price
1,apple,1.50
2,orange,2.00
```

This would produce headers: "fruit id", "fruit name", "fruit price"

With several header rows, a label carries forward to the empty cells to its right in the same header row; the labels of each column are then joined across the header rows with [`headerJoin`](#headerjoin). A cell still empty after that, such as an empty first cell, adds nothing to the join: its column's name joins only the labels that are not empty.

For example with no headers:

```json
{
  "fileDialect": {
    "format": "csv",
    "headerRows": false
  }
}
```

#### `headerJoin`

> [!NOTE]
> Supported formats: **csv**, **tsv**, **json**, **jsonl**, **xlsx**, **ods**

It `MUST` be a string. This property specifies how multiline-header files have to join the resulting header rows. It defaults to a single space (`" "`).

For example:

```json
{
  "fileDialect": {
    "format": "csv",
    "headerRows": [1, 2],
    "headerJoin": "_"
  }
}
```

For a file like:

```
fruit
id,name,price
1,apple,1.50
```

This would produce headers: "fruit_id", "fruit_name", "fruit_price"

#### `commentRows`

> [!NOTE]
> Supported formats: **csv**, **tsv**, **json**, **jsonl**, **xlsx**, **ods**

It `MUST` be an array of positive integers starting from `1`. This property specifies what rows have to be omitted from the data. In JSON and JSONL with [`rowType`](#rowtype) `object`, rows have no fixed order of cells to comment, so `commentRows` does not apply.

For example:

```json
{
  "fileDialect": {
    "format": "csv",
    "commentRows": [1, 5, 10]
  }
}
```

For a file like:

```
id,name
# This is a comment row
1,apple
2,orange
```

With `"commentRows": [2]`, the second row would be skipped.

#### `commentPrefix`

> [!NOTE]
> Supported formats: **csv**, **tsv**, **json**, **jsonl**, **xlsx**, **ods**

It `MUST` be a string. This property specifies what rows have to be omitted from the data based on the row's prefix.

For example:

```json
{
  "fileDialect": {
    "format": "csv",
    "commentPrefix": "#"
  }
}
```

For a file like:

```
id,name
# This row is ignored
1,apple
# Another comment
2,orange
```

Rows starting with `#` will be skipped.

#### `columnNames`

> [!NOTE]
> Supported formats: **csv**, **tsv**, **json**, **jsonl**, **xlsx**, **ods**

It `MUST` be an array of strings. This property specifies explicit column names to use instead of deriving them from the file.

With rows of cells, the n-th name is the name of the n-th column. Fewer names than the file has columns is an error; more names than the file has columns add columns that are `null` in every row. With [`rowType`](#rowtype) `object`, the names select the keys to read and give their order.

For example:

```json
{
  "fileDialect": {
    "format": "csv",
    "headerRows": false,
    "columnNames": ["id", "name", "price"]
  }
}
```

For a file without headers:

```
1,apple,1.50
2,orange,2.00
```

#### `jsonPointer`

> [!NOTE]
> Supported formats: **json**

It `MUST` be a string in [JSON Pointer format (RFC 6901)](https://datatracker.ietf.org/doc/html/rfc6901). This property specifies where a data is located in the document. The empty string `""` points to the whole document, and a key containing `~` or `/` is written with `~0` or `~1` in their place.

For example:

```json
{
  "fileDialect": {
    "format": "json",
    "jsonPointer": "/data/items"
  }
}
```

For a JSON file like:

```json
{
  "metadata": { "version": "1.0" },
  "data": {
    "items": [
      { "id": 1, "name": "apple" },
      { "id": 2, "name": "orange" }
    ]
  }
}
```

#### `rowType`

> [!NOTE]
> Supported formats: **json**, **jsonl**

It `MUST` be one of the following values: `array`, `object`. This property specifies whether the data items are arrays or objects. Without it, the first item decides: an array means `array`, anything else `object`.

With `array` rows, a row with more items than the table has columns cannot be read, as a long row of [CSV](#csv): loading the file `MUST` fail, and validating it `MUST` report that the file cannot be loaded. It is never truncated to the header.

With `object` rows, the columns are the union of the rows' keys, in the order each key is first seen, and a row without a key is `null` in its column. [`columnNames`](#columnnames), when present, selects and orders the columns instead.

For example with array of objects:

```json
{
  "fileDialect": {
    "format": "json",
    "rowType": "object"
  }
}
```

For data like:

```json
[
  { "id": 1, "name": "apple" },
  { "id": 2, "name": "orange" }
]
```

For example with array of arrays:

```json
{
  "fileDialect": {
    "format": "json",
    "rowType": "array",
    "columnNames": ["id", "name"]
  }
}
```

For data like:

```json
[
  [1, "apple"],
  [2, "orange"]
]
```

#### `sheetNumber`

> [!NOTE]
> Supported formats: **xlsx**, **ods**

It `MUST` be a positive integer, counting sheets from `1`. This property specifies a sheet number of a table in the spreadsheet file. If not provided, a first sheet is used. Sheets are counted in the order of the workbook, hidden sheets included, so the first sheet is the first of the workbook whether or not it is hidden.

For example:

```json
{
  "fileDialect": {
    "format": "xlsx",
    "sheetNumber": 2
  }
}
```

This reads the second sheet from the spreadsheet.

#### `sheetName`

> [!NOTE]
> Supported formats: **xlsx**, **ods**

It `MUST` be a string. This property specifies a sheet name of a table in the spreadsheet file. A dialect `MUST NOT` set both `sheetName` and [`sheetNumber`](#sheetnumber).

For example:

```json
{
  "fileDialect": {
    "format": "xlsx",
    "sheetName": "Data Sheet"
  }
}
```

#### `tableName`

> [!NOTE]
> Supported formats: **sqlite**

It `MUST` be a string. This property specifies a name of the table in the database. If not provided, a first table is used (sorted by name in ascending order).

For example:

```json
{
  "fileDialect": {
    "format": "sqlite",
    "tableName": "measurements"
  }
}
```

## Common

Common properties shared by multiple entities in the descriptor.

### External Path

It `MUST` be a string representing an HTTP or HTTPS URL to a remote file.

For example:

```json
{
  "data": "https://example.com/datasets/measurements.csv"
}
```

## Extension

> [!TIP]
> Additional properties are allowed.

Properties not defined by this specification are allowed, so that a descriptor written for a later version stays readable: an implementation `MUST` ignore them and `MAY` warn about them. Fairspec File Dialect has no extension mechanism: such properties carry no meaning.

## Comparison

> [!NOTE]
> This section is under development.
