Skip to main content
Dev & Data Popular

JSON to CSV & CSV to JSON Bi-Directional Converter

Convert nested or flat JSON arrays into standard RFC 4180 CSV spreadsheets, or transform CSV and TSV text into typed JSON objects with automatic type inference and delimiter detection.

Bi-directional conversion: Instant JSON-to-CSV and CSV-to-JSON with auto-sync and real-time validation
RFC 4180 compliant CSV parser handling escaped double quotes, line breaks within cells, and custom delimiters (comma, semicolon, tab, pipe)
Deep object flattening and unflattening for nested JSON properties using dot-notation (e.g., user.address.city)
Automatic data type inference detecting numbers, booleans, nulls, and ISO timestamps from raw CSV strings
Interactive tabular data preview with one-click download as .csv or .json files
Sponsored Ad Zone

Clean, non-intrusive developer tools sponsor zone. Zero cumulative layout shift.

Comprehensive Technical Manual

The Architectural Guide to Bi-Directional JSON & CSV Data Transformation

In-depth specifications, architectural mechanics, real-world code implementations, and industry best practices.

01

Understanding the RFC 4180 CSV Specification

  • Comma-Separated Values (CSV) is standardized by IETF RFC 4180. Key requirements include:
  • Delimiters: Fields are separated by commas (or semicolons in European locales).
  • Quoting Rules: Any field containing commas, line breaks (CRLF), or double quotes must be wrapped in double quotes.
  • Escaping Quotes: A literal double quote inside a quoted field is escaped by doubling it ("").
  • UTF-8 BOM: Prepending a Byte Order Mark (\uFEFF) ensures Microsoft Excel properly renders non-ASCII Unicode characters (accents, Asian scripts, emojis).
Implementation Example
// RFC 4180 Compliant CSV Line Escaping in TypeScript
export function escapeCsvField(val: unknown): string {
  if (val === null || val === undefined) return '';
  const str = typeof val === 'object' ? JSON.stringify(val) : String(val);
  if (str.includes(',') || str.includes('"') || str.includes('\n') || str.includes('\r')) {
    return `"${str.replace(/"/g, '""')}"`;
  }
  return str;
}
02

Handling Nested JSON Objects: Dot-Notation Flattening & Unflattening

JSON supports deep hierarchical structures (nested objects and arrays), whereas CSV is inherently two-dimensional (rows and columns). To convert nested JSON into flat CSV, nested keys are flattened using dot-notation (e.g., {"user": {"profile": {"age": 30}}} becomes user.profile.age). During CSV-to-JSON conversion, dot-notation columns are reconstructed into nested JavaScript objects.

Implementation Example
// Deep Object Flattening Utility in TypeScript
export function flattenObject(obj: Record<string, any>, prefix = ''): Record<string, any> {
  return Object.keys(obj).reduce((acc: Record<string, any>, k: string) => {
    const pre = prefix.length ? `${prefix}.` : '';
    if (typeof obj[k] === 'object' && obj[k] !== null && !Array.isArray(obj[k])) {
      Object.assign(acc, flattenObject(obj[k], pre + k));
    } else {
      acc[pre + k] = obj[k];
    }
    return acc;
  }, {});
}
03

Step-by-Step TypeScript Implementation: Robust CSV-to-JSON Parser

A production-ready CSV tokenizer maintains state machine tracking for in-quotes vs out-of-quotes characters, accurately preserving embedded line breaks and escaped quotes without fragile regex splits.

Implementation Example
// Robust State-Machine CSV to Array of Arrays Parser
export function parseCsvRows(csvText: string, delimiter = ','): string[][] {
  const rows: string[][] = [];
  let currentRow: string[] = [];
  let currentField = '';
  let insideQuotes = false;

  for (let i = 0; i < csvText.length; i++) {
    const char = csvText[i];
    const nextChar = csvText[i + 1];

    if (char === '"') {
      if (insideQuotes && nextChar === '"') {
        currentField += '"';
        i++; // Skip escaped quote
      } else {
        insideQuotes = !insideQuotes;
      }
    } else if (char === delimiter && !insideQuotes) {
      currentRow.push(currentField);
      currentField = '';
    } else if ((char === '\r' || char === '\n') && !insideQuotes) {
      if (char === '\r' && nextChar === '\n') i++;
      currentRow.push(currentField);
      if (currentRow.some((f) => f.length > 0)) rows.push(currentRow);
      currentRow = [];
      currentField = '';
    } else {
      currentField += char;
    }
  }
  if (currentField.length > 0 || currentRow.length > 0) {
    currentRow.push(currentField);
    rows.push(currentRow);
  }
  return rows;
}
04

Automatic Data Type Inference: Numbers, Booleans, Nulls & Dates

Because raw CSV contains only string values, converting CSV to JSON requires intelligent type coercion. The engine checks whether a value matches boolean literals (true/false), integer/floating-point numbers, ISO 8601 date strings, or null representations, restoring original typed representations in the generated JSON payload.

Implementation Example
// Safe Type Inference Helper
export function inferType(val: string): string | number | boolean | null {
  const trimmed = val.trim();
  if (trimmed === '' || trimmed.toLowerCase() === 'null') return null;
  if (trimmed.toLowerCase() === 'true') return true;
  if (trimmed.toLowerCase() === 'false') return false;
  if (!isNaN(Number(trimmed)) && !trimmed.startsWith('0x') && trimmed !== '') {
    return Number(trimmed);
  }
  return val;
}
05

Spreadsheet Compatibility & Formula Injection (CSV Injection) Security

When CSV files are opened in spreadsheet software (Microsoft Excel, Google Sheets, LibreOffice), cells beginning with =, +, -, or @ can trigger formula execution (CSV Injection / DDE vulnerability). When generating CSV files containing user-submitted data, prefix sensitive formula characters with a single quote (') or escape them to protect downstream users.

Implementation Example
// Sanitizing Formula Injection Vectors in CSV Export
export function sanitizeCsvFormula(value: string): string {
  if (/^[=+\-@\t\r]/.test(value)) {
    return `'${value}`; // Prepend single quote to neutralize formula
  }
  return value;
}
Knowledge Base & Clarifications

Frequently Asked Questions: JSON / CSV Converter

Got questions about how JSON / CSV Converter operates, client-side cryptographic safety, or performance limits? Explore common answers below.

Complementary Utilities
View all in Dev & Data →