What changes when data is converted between JSON, CSV, YAML and XML?
The four formats model data differently: JSON and YAML carry nesting and types, CSV is a flat table of strings, and XML distinguishes elements from attributes. A converter therefore has to make choices — how to flatten nested objects into columns, how to type "42" and "true", where attributes go — and the right choice depends on what the receiving system expects.
How to use XML to JSON
- Paste the XML document.
- Decide whether to keep attributes, parse numbers/booleans and which elements must always be arrays.
- Copy or download the JSON.
<product> becomes an object and two become an array; listing product under Always-array elements gives an array every time.Occurs once
One <product> inside <catalog>
"product": {
"name": "Keyboard",
"@_id": "p-1"
}
- Always-array elements:
- Empty
productin the JSON:- Object
Repeated
Two sibling <product> elements, as in the example
"product": [
{
"name": "Keyboard",
"@_id": "p-1"
},
{
"name": "Mouse",
"@_id": "p-2"
}
]
- Always-array elements:
- Empty
productin the JSON:- Array of 2
Once, forced to an array
The same single <product>, with product listed as always-array
"product": [
{
"name": "Keyboard",
"@_id": "p-1"
}
]
- Always-array elements:
productproductin the JSON:- Array of 1
Each code box is the entry under "catalog" in the tool’s own output for that XML, with the default options; the attribute id becomes "@_id".
XML to JSON features
- Well-formedness check with line and column for malformed XML
- Attributes preserved with the @_ prefix (or dropped on request)
- Repeated children become arrays; force arrays for specific element names
- Optional conversion of numeric and boolean text values
- CDATA content and entities decoded
- Summary of root element, element count and depth
XML to JSON example
Catalog with attributes and repeated elements
Input:
<catalog><product id="p-1"><name>Keyboard</name></product><product id="p-2"><name>Mouse</name></product></catalog>Output:
{
"catalog": {
"product": [
{ "name": "Keyboard", "@_id": "p-1" },
{ "name": "Mouse", "@_id": "p-2" }
]
}
}Frequently asked questions about XML to JSON
How are attributes represented?
Attributes become keys prefixed with @_ (e.g. "@_id"). If an element has both attributes and text, the text is stored under "#text". You can also drop attributes entirely.
Why is one element an object but another an array?
Repeated sibling elements are collected into an array; an element that occurs once stays an object. List element names under "Always-array elements" to force a consistent array shape.
Are numbers and booleans converted?
By default text such as 42, 3.5 and true is converted to JSON numbers and booleans. Disable "Parse numbers & booleans" to keep every value as a string (useful for zip codes and IDs with leading zeros).
What about CDATA, comments and namespaces?
CDATA content is included as text, comments and processing instructions are dropped, and namespaced names such as soap:Body are kept verbatim as keys.
References
- RFC 8259: The JavaScript Object Notation (JSON) Data Interchange FormatThe JSON grammar: objects, arrays, strings, numbers, the three literals, and UTF-8 as the interchange encoding.
- RFC 4180: Common Format and MIME Type for Comma-Separated Values (CSV) FilesThe commonly implemented CSV rules: comma separators, CRLF line ends, quoting of fields that contain commas, quotes or line breaks.
- YAML Ain't Markup Language (YAML™) version 1.2.2The YAML 1.2 data model and syntax, and its relationship to JSON (every JSON document is valid YAML 1.2).
- Extensible Markup Language (XML) 1.0 (Fifth Edition)What makes an XML document well-formed: elements, attributes, entities, character encoding and the prolog.