JSON, YAML, XML and CSV: Where Format Conversions Quietly Lose Your Data
A code-level look at what actually changes when data moves between JSON, YAML, XML and CSV: string coercion, nested objects, repeated XML tags, and YAML's boolean surprises.
What this guide covers
- 1. Four formats, four different data models
- 2. CSV: every value becomes a string
- 3. XML: attributes disappear, and one child is not the same as two
- 4. YAML: implicit typing and the Norway problem
- 5. JSON is the strict one, and that is a feature
- 6. A round-trip test that actually catches the loss
- 7. Choosing a format on purpose
- 8. Frequently asked questions
- 9. The one-line version
A format converter looks like a translator: same content, different spelling. That model is wrong in a specific and expensive way. JSON, YAML, XML and CSV do not describe the same kind of thing, so moving between them is not translation — it is a projection, and projections lose information.
The Data Converter on this site supports all four in every direction. Its implementation is a useful specimen because it is small enough to read in full and honest enough to show exactly where the loss happens. What follows is a map of those seams, so you can predict the result before you paste a customer's spreadsheet into a production import.
Four formats, four different data models
Before any conversion, it helps to say what each format can natively express. The gaps between these lists are where your data goes missing.
- JSON: objects, arrays, strings, numbers, booleans and null. Ordered object keys in practice, no comments, no dates as a distinct type.
- YAML: a superset in spirit — nested maps, sequences, scalars, plus implicit typing that guesses whether a scalar is a number, a boolean or a timestamp.
- XML: an ordered tree of elements with text content and attributes. It has no native concept of an array, a number or a boolean; everything is text until a schema says otherwise.
- CSV: a flat table of rows, each row a list of strings. No nesting, no types, no null distinct from empty string, no metadata about what any column means.
Notice that CSV is the narrowest by a wide margin and YAML is the most ambitious. Converting from a narrow model into a rich one adds nothing back: a value that arrived as text cannot be reconstructed as a number with any confidence. Converting the other way has to throw structure away.
CSV: every value becomes a string
The CSV reader here builds each row with a line equivalent to `o[header || colN] = cells[i] ?? ''`. There is no type inference at all. Paste `name,age,active` followed by `Ada,42,true` and the resulting objects are `{ name: 'Ada', age: '42', active: 'true' }` — strings, every one of them. Convert that straight to JSON and you get quoted numbers and a quoted boolean. Downstream code that checks `age > 18` may still appear to work because JavaScript coerces, while code that checks `active === true` silently never matches.
- Empty and missing collapse. An absent trailing cell becomes `''`, and a cell that literally contained an empty string also becomes `''`. You cannot distinguish 'not provided' from 'provided as blank' after the round trip.
- Blank headers get synthetic names. An empty column heading becomes `col0`, `col1` and so on, keyed by position. Rename a column in the source and every downstream mapping breaks.
- Extra cells are dropped. The row builder iterates over the header array, so a row with more cells than the header quietly loses the surplus.
- Whitespace-only lines are discarded before parsing, which is usually helpful and occasionally removes a meaningful blank record.
The writer has its own set of decisions. It collects the union of all keys across all rows into a `Set`, so column order follows first appearance and every row is padded to the same width. Values containing a comma, a double quote or a newline get quoted, with internal quotes doubled. Values that are `null` or `undefined` become an empty cell. And because the quoting helper calls `String(x)` on whatever it receives, a nested object serialises as the literal text `[object Object]` and an array such as `[1, 2]` becomes the string `1,2` (then gets quoted because of the comma).
- Flatten before you convert. Turn nested objects into explicit scalar columns with names like `address_city`. Do not let `String()` make that decision for you.
- Check the union. If one row has a key no other row has, the output gains a column that is empty almost everywhere. That is correct behaviour and a common surprise.
- Re-read the output. A five-second look at the generated CSV catches `[object Object]` before it reaches an import job.
XML: attributes disappear, and one child is not the same as two
The XML reader here walks child elements and reads `textContent`. It never looks at attributes. `<user id='42'>Ada</user>` becomes `{ user: 'Ada' }` and the identifier is simply gone. If your XML carries ids, statuses, units or language tags as attributes — and most real XML does — a conversion that ignores them is lossy in a way that is easy to miss because the output still looks plausible.
The second XML trap is structural. When the reader meets repeated sibling tags, the first occurrence is stored as a scalar and the second causes it to be replaced by an array; a third appends. So a document with one `<item>` produces a single object, while the same document with two `<item>` elements produces an array. Consumer code written against the one-item sample breaks on the two-item real data. This is the single most common XML-to-JSON production bug, and it is not a parser defect — XML has no array type, so every converter has to invent a rule.
- Attributes are not read. Move them into child elements before converting, or use a schema-aware converter.
- Mixed content (text with elements interleaved) collapses into concatenated text; the element boundaries disappear.
- The root is wrapped: the result is keyed by the root tag name, adding one level you may not expect.
- On the way out, key names are sanitised — invalid characters become `_`, names starting with a digit get a leading `_`, and an empty name becomes `field`. `null` becomes a self-closing element and arrays become repeated sibling tags rather than a wrapping container.
YAML: implicit typing and the Norway problem
YAML readers guess types. That convenience has a famous failure mode: in YAML 1.1, the unquoted scalars `yes`, `no`, `on`, `off`, `y` and `n` are booleans. A country code list containing `NO` for Norway can be parsed as `false`. Quoted values are safe, but you rarely control the file you were handed. Dates are similarly ambiguous — an unquoted `2026-11-10` may become a Date object rather than a string — and values like `0777` or `1e3` can change type on the way in.
On the output side this converter calls `yaml.dump(value, { noRefs: true })`. That option deliberately expands repeated references into full copies instead of emitting anchors and aliases. The result is larger but more portable, because the reader does not need to support alias resolution. It is a good default for data that will be pasted into another tool; it is a poor default if you are round-tripping a large object graph where identity mattered.
JSON is the strict one, and that is a feature
The JSON path is a bare `JSON.parse` on input and `JSON.stringify(value, null, 2)` on output. `parse` rejects trailing commas, comments, single quotes, `NaN`, `Infinity` and `undefined`. The other three formats are all more permissive, which means YAML → JSON and XML → JSON usually succeed while JSON → YAML is the direction most likely to fail on hand-edited input. That asymmetry is worth remembering when a conversion chain suddenly breaks halfway through.
`stringify` has its own quiet edits: `undefined` properties vanish, `NaN` and `Infinity` become `null`, `Date` objects become ISO strings, and functions are dropped. None of these raise an error. If a field disappears after a round trip, this is where to look first. Use the JSON Validator when you need a strict pass or fail answer rather than a pretty print, and the JSON Formatter when you only need to read the shape.
A round-trip test that actually catches the loss
Comparing the converted text to the original text proves nothing — different spacing, key order and quoting will always differ. Compare the structure instead, and do it before the data touches anything important.
- 1. Convert A → B → A with the Data Converter and diff the two A representations semantically, not as strings. Types, nulls and array-versus-scalar shape are what matter.
- 2. Include the awkward row. One nested object, one empty value, one numeric-looking string, one repeated XML tag, one `yes` or `NO` scalar. A sample with only clean flat strings will pass every test and tell you nothing.
- 3. Count the rows and keys. Silent row loss (extra CSV cells, whitespace-only lines) and silent column gain (the union rule) are both visible in a count.
- 4. Decide the types before importing. If a column is an integer, cast it explicitly at the boundary. Do not rely on a formatter to have guessed for you.
Choosing a format on purpose
- Config with nesting and humans editing it: YAML, with codes quoted and a linter in CI.
- APIs and storage: JSON. Strictness is what makes it predictable across languages.
- Document interchange with an existing schema or attributes: XML, with a real parser that reads attributes.
- Spreadsheets, flat exports and bulk imports: CSV, with an explicit schema describing each column's type and null handling.
Frequently asked questions
Why did my numbers come back as strings? CSV has no type system. Every cell is text until you declare otherwise.
Why is there a column full of blanks? The CSV writer emits the union of all keys across all rows, so a key present in one row appears as an empty cell in every other row.
Why did my nested object become `[object Object]`? The quoting helper converts values with `String()`. Flatten nested structures into scalar columns first.
Is a round trip lossless? Between JSON and YAML, usually, for simple data. Any path through CSV or XML is lossy by construction.
Does the conversion upload my file? No. The parser and serialiser run in your browser; nothing is sent to a server. That matters most when the spreadsheet contains customer data.
The one-line version
Treat every conversion as lossy until you have proved otherwise on a deliberately awkward sample. CSV flattens and stringifies, XML drops attributes and invents arrays, YAML guesses types, and JSON quietly discards what it cannot represent. The format you choose is a data model decision, not a file extension.
Tools run privately in your browser
Every tool referenced in this guide processes your input in the browser rather than uploading it to a server we operate, and requires no account. A typical single job stays free. Larger batches, history export, and an ad-free page are optional on Pro. A few tools load a processing engine from a CDN on first use; see the privacy policy for details. Browse the full toolbox →