How to Convert Between Data Formats: JSON, CSV, XML, YAML, and SQL
A practical guide to converting between the most common data formats. When to use each format, conversion gotchas, and step-by-step examples for JSON, CSV, XML, YAML, SQL, and TOML.
The Data Format Landscape
Every developer works with multiple data formats daily. APIs return JSON, spreadsheets export CSV, configuration files use YAML or TOML, legacy systems speak XML, and databases store SQL. Knowing how to convert between them efficiently is a core skill.
JSON (JavaScript Object Notation)
JSON is the lingua franca of web APIs. It's human-readable, lightweight, and natively supported in JavaScript.
{
"users": [
{"id": 1, "name": "Alice", "email": "alice@example.com", "active": true},
{"id": 2, "name": "Bob", "email": "bob@example.com", "active": false}
]
}
Strengths: Universal API support, native JS parsing, good tooling Weaknesses: No comments, no date type, verbose for simple data
Format and validate JSON with our JSON Formatter, or validate syntax with our JSON Validator.
CSV (Comma-Separated Values)
CSV is the simplest tabular format. Every spreadsheet app can read and write it.
id,name,email,active
1,Alice,alice@example.com,true
2,Bob,bob@example.com,false
Strengths: Universal spreadsheet support, tiny file size, human-readable Weaknesses: No nested data, no types, delimiter conflicts, no standard spec
JSON ↔ CSV Conversion
Converting JSON arrays to CSV is straightforward when the data is flat:
// JSON to CSV
function jsonToCsv(data) {
const headers = Object.keys(data[0]);
const rows = data.map(row =>
headers.map(h => {
const val = String(row[h] ?? "");
return val.includes(",") ? `"${val}"` : val;
}).join(",")
);
return [headers.join(","), ...rows].join("\n");
}
Gotchas:
- Nested objects must be flattened or stringified
- Commas in values need quoting
- Newlines in values need quoting
- Different locales use semicolons instead of commas
Convert instantly with our CSV to JSON and JSON to CSV converters.
XML (Extensible Markup Language)
XML was the dominant data format before JSON. It's still widely used in enterprise systems, SOAP APIs, RSS feeds, and configuration files.
<users>
<user id="1">
<name>Alice</name>
<email>alice@example.com</email>
<active>true</active>
</user>
<user id="2">
<name>Bob</name>
<email>bob@example.com</email>
<active>false</active>
</user>
</users>
Strengths: Schema validation (XSD), namespaces, attributes, XSLT transforms Weaknesses: Verbose, complex parsing, heavy
JSON ↔ XML Conversion
The main challenge is that XML has features JSON doesn't: attributes, namespaces, mixed content, and CDATA sections.
// Simple JSON to XML
function jsonToXml(obj, rootName = "root") {
function convert(data, name) {
if (data === null) return `<${name}/>`;
if (typeof data !== "object") return `<${name}>${escapeXml(String(data))}</${name}>`;
if (Array.isArray(data)) return data.map(item => convert(item, name)).join("");
return `<${name}>${Object.entries(data).map(([k, v]) => convert(v, k)).join("")}</${name}>`;
}
return `<?xml version="1.0" encoding="UTF-8"?>\n${convert(obj, rootName)}`;
}
Convert with our JSON to XML and XML to JSON converters. Format XML with our XML Formatter.
YAML (YAML Ain't Markup Language)
YAML is the preferred format for configuration files: Docker Compose, Kubernetes, GitHub Actions, Ansible, and more.
users:
- id: 1
name: Alice
email: alice@example.com
active: true
- id: 2
name: Bob
email: bob@example.com
active: false
Strengths: Human-readable, comments, multi-line strings, anchors/aliases Weaknesses: Indentation-sensitive, security risks (arbitrary code execution in some parsers), complex spec
JSON ↔ YAML Conversion
JSON and YAML are largely interchangeable — valid JSON is valid YAML.
Convert with our JSON to YAML converter. Format YAML with our YAML Formatter.
SQL (Structured Query Language)
SQL is how you talk to relational databases. Converting data to SQL INSERT statements is common when seeding databases or migrating data.
CREATE TABLE users (
id INTEGER PRIMARY KEY,
name TEXT NOT NULL,
email TEXT NOT NULL,
active BOOLEAN DEFAULT true
);
INSERT INTO users (id, name, email, active) VALUES
(1, 'Alice', 'alice@example.com', true),
(2, 'Bob', 'bob@example.com', false);
Convert with our JSON to SQL and CSV to SQL converters. Format queries with our SQL Formatter.
TOML (Tom's Obvious Minimal Language)
TOML is gaining popularity for configuration files, especially in the Rust (Cargo.toml) and Python (pyproject.toml) ecosystems.
[database]
host = "localhost"
port = 5432
name = "myapp"
[[users]]
id = 1
name = "Alice"
email = "alice@example.com"
active = true
Strengths: Unambiguous, comments, datetime type, clear section headers Weaknesses: Less tooling than YAML/JSON, not great for deeply nested data
Format with our TOML Formatter.
Conversion Matrix
| From → To | JSON | CSV | XML | YAML | SQL |
|---|---|---|---|---|---|
| JSON | — | JSON→CSV | JSON→XML | JSON→YAML | JSON→SQL |
| CSV | CSV→JSON | — | Manual | Manual | CSV→SQL |
| XML | XML→JSON | Manual | — | Manual | Manual |
| YAML | YAML→JSON | Manual | Manual | — | Manual |
Best Practices
- Validate before converting — Use our JSON Validator to catch syntax errors before conversion
- Handle encoding — Always use UTF-8. Use our Base64 Encoder for binary data in text formats
- Escape special characters — Use our String Escape/Unescape tool
- Test with edge cases — Empty arrays, null values, special characters, very long strings
- Choose the right format — JSON for APIs, CSV for spreadsheets, YAML for config, XML for enterprise
All Conversion Tools
- JSON Formatter — Format and validate JSON
- JSON to YAML — Convert JSON to YAML
- JSON to XML — Convert JSON to XML
- JSON to CSV — Convert JSON to CSV
- JSON to SQL — Convert JSON to SQL INSERT
- JSON to TypeScript — Generate TS interfaces
- CSV to JSON — Convert CSV to JSON
- CSV to SQL — Convert CSV to SQL
- XML Formatter — Format XML
- YAML Formatter — Format YAML
- SQL Formatter — Format SQL
- Markdown to HTML — Convert Markdown