DEV Community

Cover image for Converting TSV to YAML: A Guide to Reliable Data Serialization
kandz
kandz

Posted on

Converting TSV to YAML: A Guide to Reliable Data Serialization

When working with configuration management systems like Ansible, Kubernetes, or Docker Compose, YAML is the standard. But data often starts in spreadsheets, CSV exports, or tab-separated values (TSV) from databases.

Manually converting TSV to YAML is error-prone. A single misplaced tab, an unescaped colon, or a missing hyphen can break your entire configuration.

In this guide, I'll walk through how to convert TSV data to YAML reliably.


1. Understanding TSV and YAML

TSV (Tab-Separated Values) is a simple file format where each row is a record and columns are separated by tabs. It's commonly exported from Excel, Google Sheets, and SQL databases.

YAML (YAML Ain't Markup Language) is a human-readable data serialization format. It uses indentation to represent hierarchy, making it ideal for configuration files.

The challenge: TSV is flat. YAML is hierarchical. Converting between them requires mapping columns to nested structures.


2. The Parsing Problem

A naive TSV parser splits each line by tabs. But this breaks when data contains:

  • Empty fields
  • Values with tabs inside strings
  • Lines with inconsistent column counts
  • Quoted strings with embedded separators

The correct approach:

function parseTSV(csvData: string): string[][] {
  const lines = csvData.split('\n');
  const result = [];

  for (const line of lines) {
    if (!line.trim()) continue;
    const row = line.split('\t');
    result.push(row.map(cell => cell.trim()));
  }

  return result;
}
Enter fullscreen mode Exit fullscreen mode

3. Converting TSV to YAML

Once parsed, the transformation is straightforward:

function convertToYAML(tsvData: string): string {
  const rows = parseTSV(tsvData);
  if (rows.length === 0) return '';

  const headers = rows[0];
  const records = rows.slice(1);

  const yamlObj = records.map(row => {
    const obj = {};
    headers.forEach((header, index) => {
      obj[header] = row[index] || '';
    });
    return obj;
  });

  return YAML.stringify(yamlObj);
}
Enter fullscreen mode Exit fullscreen mode

4. Edge Cases to Handle

Issue Solution
Empty rows Skip them
Inconsistent columns Fill missing values with empty strings
Special characters Escape in YAML
Large datasets Process in chunks to avoid memory issues

5. Common Use Cases

  • Kubernetes ConfigMaps – Converting spreadsheet data into ConfigMap YAML
  • Ansible Inventory – Mapping host data to inventory YAML
  • Docker Compose – Building compose files from service tables
  • Database Seed Data – Exporting data to YAML fixtures

6. Client-Side Processing

All parsing and conversion runs locally in your browser. Your data never leaves your device. This is critical when working with:

  • Configuration files containing secrets
  • Production database exports
  • Internal project data

Interactive Tool

If you regularly convert TSV to YAML, I built a free tool that handles all these cases automatically:

👉 TSV to YAML Converterhttp://tools.kandz.me/tsv-to-yaml


What's your use case for converting TSV to YAML? Drop it in the comments.

Top comments (0)