No tool matches “”

Try a format (“webp”, “mp3”) or a verb (“merge”, “compress”).

Loading index

↵to open

fundamentals · 7 min read

XML, CSV and JSON: choosing and converting data formats

CSV, JSON and XML all carry data, but they make different assumptions about its shape. CSV is a flat table, JSON is a tree of objects and arrays, and XML is a document format built from nested elements, attributes and optional schemas.

Choosing well comes down to where the data goes next: a spreadsheet, an API or a system that expects a schema. Conversions are mechanical, but headers, types and nesting are easy to lose, so a quick validation step belongs in the workflow rather than being left until something breaks.

Tabular and hierarchical data are different shapes

CSV treats data as rows and columns. Every row has the same fields, a header row names them, and values are separated by a delimiter. That shape is ideal for spreadsheets, bulk imports, logs and anything that can be analyzed one row at a time.

JSON and XML treat data as a tree. A record can hold nested objects, repeated lists and values that only some records have. That is a better match for API responses, configuration and documents with mixed content. The two models can represent similar data, but mapping one to the other is a decision, not a mechanical equivalence.

Most real datasets mix both shapes. An order has a flat set of identifiers, a customer object and a list of line items. Serializing that to CSV forces a choice: repeat the order fields on every line item, write the line items as a JSON string inside a cell, or split the export into multiple tables. None of those choices is wrong, but each one loses or changes something, and the flattening rule should be written down.

CSV: headers, quoting and line breaks

CSV has a widely used reference in RFC 4180, but many files in circulation are only CSV-like. The delimiter is usually a comma, though semicolons and tabs are common, and the first row may or may not be a header. A parser that guesses the delimiter can be convenient for human-written files and dangerous for machine-generated ones, so check the result before trusting it.

Quoting is the part that most often goes wrong. A field containing a comma, a double quote or a line break must be wrapped in double quotes, and a double quote inside a quoted field is written twice. If that rule is missed, a single address such as "Main Street, Suite 4" splits into two columns and every later value shifts. Embedded newlines are legal inside a quoted field but many simple line-splitting scripts cannot handle them.

CSV stores no types: every value is text until a program decides otherwise. A CSV writer may accidentally turn a phone number or a zip code into a number and drop leading zeros. When converting to JSON or a database, decide the type per column deliberately and keep identifiers as strings even when they look numeric.

XML: elements, attributes and schemas

XML represents data with elements, attributes and text content. An element can contain text, other elements, or both, which makes XML good for documents where markup and content are mixed. Attributes are usually used for metadata such as an ID or a unit, while child elements carry the data itself, but that split is a design choice rather than a rule of the format.

A document is well-formed when it follows XML's syntax rules: tags close correctly, attribute values are quoted, one root element wraps everything, and special characters are escaped. It is valid when it also conforms to a DTD or XML Schema. Validation catches structural mistakes that a formatter alone will not, which is why schema-aware systems reject files that look perfectly indented.

Namespaces prevent collisions between vocabularies, but they complicate naive conversions because an element's name is its namespace plus its local name. Whitespace and comments are usually not data, yet they can appear in a serialized document. Converting XML to CSV or JSON always involves a mapping decision about attributes, repeated elements and mixed content, and the source schema is the best guide to that decision.

JSON: objects, arrays and strict syntax

JSON has six value types: object, array, string, number, boolean and null. Objects have string keys, arrays are ordered lists, and the syntax is strict: keys and strings use double quotes, comments are not allowed, and trailing commas are errors. That strictness makes the format predictable to parse, but it also means a file that looks fine in a loose JavaScript context can be rejected by a validator.

The number type is a floating-point value in the JSON specification, so very large integers and high-precision decimals can lose precision in tools that store numbers as doubles. Identifiers that happen to be numeric should be quoted as strings when exact preservation matters. Duplicate keys are undefined behavior in many parsers, so they should be treated as a data error rather than a style issue.

JSON Schema gives JSON a validation mechanism comparable to XML Schema, but it is optional and separate from the data file. For streaming data, formats such as JSON Lines wrap one JSON value per line; that differs from a single JSON array and must be handled consciously.

Type inference is the main conversion hazard

When CSV becomes JSON, a converter must decide which text values are numbers, booleans or nulls. Rules differ between tools: an empty cell might become an empty string, null or a missing key; a value like 007 might become 7; words such as true and null might be converted because they look special. The same column can therefore produce different JSON from two converters.

The reverse direction has similar traps. A JSON object with nested arrays cannot be flattened to CSV without a rule for names, row expansion and missing values. XML attributes may disappear if the converter only reads child elements, and repeated elements may collapse to the last value. A JSON-to-TypeScript converter infers a static shape from the sample, so optional fields, nullability and unions are estimates that need review, not runtime validation.

The reliable approach is to fix the mapping before converting: which columns are strings, what an empty value means, how nested lists are represented and what happens to XML attributes. Keeping the source file makes it possible to compare the original and converted values instead of discovering the difference in a downstream system.

When each format fits

CSV is the right choice when the data is genuinely tabular and the consumer is a spreadsheet, a bulk import or a simple analysis script. Its limitations are hierarchy, types and metadata; it has no way to describe column types or relationships, and its quoting rules are often implemented loosely. For a flat export that a person will open in Excel, it is still hard to beat.

JSON is a natural fit for web APIs, configuration and JavaScript-heavy tooling, especially when the data is nested or the schema can change. It is less comfortable for mixed document content, comments and very large tabular datasets, where its repeated keys add size and a stream-oriented format may be better.

XML remains common where schemas, namespaces, mixed content or legacy integrations matter. It is verbose compared with JSON, but that verbosity carries structure and validation rules that other formats leave to the application. The question is which system has to read the data and what guarantees it needs, not which format is trending.

Convert, then validate the result

Start by inspecting the source with a formatter or validator rather than converting a file you have not checked. The XML Formatter and JSON Formatter report syntax errors with a line and column, which prevents a malformed input from producing a confusing output. For CSV, check the delimiter, the header row and any quoted fields with embedded commas or newlines.

After conversion, compare the shape and the values: record counts, column names, empty fields, nested lists and strings that look numeric. Round-trip a sample back to the original format when possible; a successful round trip is not proof, but a mismatch is a useful signal. Validate against a schema or a TypeScript type check where the destination expects one, and remember that formatting makes data readable, not correct.

Use the converter that matches the direction. CSV to JSON turns a header row into object keys and handles quoted fields, which is useful for API payloads. JSON to TypeScript generates interfaces from a sample for typed code, but the generated types still need review. All of these tools run locally in the browser, so sensitive sample data does not need to leave the machine.

  • Validate the source before converting it.
  • Compare record counts, headers and empty values after converting.
  • Keep identifiers as strings when leading zeros or precision matter.
  • Round-trip a small sample to catch mapping mistakes.

Frequently asked questions

Which format should I use: CSV, JSON or XML?
Use CSV for flat tabular data headed to a spreadsheet or bulk import, JSON for nested data and web APIs, and XML when a schema, namespaces or mixed document content matter. The deciding factor is what reads the data next and what structure it needs to preserve, not which format is newer.
Can CSV represent nested data?
CSV has no native nesting. You can flatten one-to-many relationships by repeating parent values on every row, serialize nested parts as JSON inside a cell, or split the data into several tables. Each option changes how the data is queried and parsed, so the chosen convention should be documented for whoever consumes the file.
Why do leading zeros disappear when I open a CSV in Excel?
Excel infers a number from a value like 007 and formats it as 7, because CSV itself carries no type information. Import the column as text, or keep identifiers in a format the spreadsheet preserves, before doing the conversion. The same issue appears when a CSV-to-JSON converter guesses a numeric type.
Are XML attributes the same as child elements?
No. Attributes and child elements are different syntax with different rules, and a mapping from XML to JSON or CSV has to decide what happens to each. Attributes commonly hold metadata such as IDs or units, while child elements hold data, but that is a convention. Check the source schema before flattening either one.
How do I convert JSON to TypeScript without losing accuracy?
Generate interfaces from a representative sample, then review fields that may be optional or null and any union of shapes that appears only in other records. A generated interface describes the sample, not the API contract. For runtime safety, validate incoming JSON against a schema or runtime validator rather than relying on the type alone.
Does formatting or validating a file change its data?
Formatting should only change whitespace and indentation, not values, but it will fail or alter a file that is malformed. A formatter is a syntax check, not a schema check. Validate the structure separately if the destination expects a specific schema, and keep a copy of the original before any conversion.

Tools for this job

All Developer Tools