Convert JSON to well-formed XML instantly — with CDATA, null handling, and custom element names
JSON (JavaScript Object Notation) and XML (Extensible Markup Language) are the two most widely used data interchange formats in modern software development. While JSON dominates REST APIs and web applications due to its lightweight syntax and native JavaScript compatibility, XML remains the backbone of enterprise integrations, SOAP web services, configuration files, document formats like DOCX and SVG, and countless legacy systems that predate the JSON era. Converting between these two formats is a routine task for developers, data engineers, systems integrators, and technical writers. You might need to send data from a JSON-based microservice to a legacy SOAP endpoint, feed a modern API's response into an XML-driven workflow engine, transform a configuration object exported from a JavaScript application into an XML configuration file, or simply inspect complex nested data in a more verbose, self-describing format that XML provides. Our JSON to XML Converter handles the full range of conversion scenarios you encounter in real-world work. Simply paste your JSON text into the editor or upload a .json file up to 2 MB, adjust the formatting options to match your target system's expectations, and get properly indented, well-formed XML output in seconds. The converter auto-updates as you type, so there is no need to click a button for every change — results appear with a 300-millisecond debounce to keep the interface responsive even for large inputs. The tool supports all JSON data types correctly. Objects become XML element trees where each key is a child element. Arrays are rendered as repeated sibling elements under a configurable tag name (defaulting to 'item'). Primitive values — strings, numbers, and booleans — become text node content. Null values can be omitted entirely, rendered as self-closing empty elements, or written as explicit null text, depending on your requirements. XML element names have strict rules that JSON keys do not: they cannot start with a digit, cannot contain spaces or most special characters, and cannot begin with the reserved string 'xml'. Our converter automatically sanitizes any key that violates these rules, replacing spaces with underscores, prefixing digit-leading names with an underscore, and stripping unsupported characters. A warning is shown whenever sanitization occurs so you always know if your output differs from the original key names. For strings containing HTML or XML special characters — angle brackets, ampersands, quotes — you can choose between automatic XML escaping (converting < to < and so on) or CDATA section wrapping, which preserves the raw string inside a CDATA block and is often easier to read and process downstream. The hyphen-prefix convention (keys starting with '-') is also supported: those keys are rendered as XML attributes on the parent element rather than child elements, following the convention used by several XML serialization libraries. Formatting is fully configurable: choose 2-space or 4-space indentation, tab indentation, or compact/minified output with no whitespace — useful when you need to minimize payload size. The XML declaration line (<?xml version="1.0" encoding="UTF-8"?>) can be toggled on or off to match the expectations of your target parser. NDJSON (newline-delimited JSON) is also detected automatically, with each line parsed as a separate record and wrapped under the root element.
Understanding JSON to XML Conversion
What Is JSON to XML Conversion?
JSON to XML conversion is the process of transforming data encoded in JavaScript Object Notation into an equivalent representation using Extensible Markup Language. JSON uses key-value pairs, arrays, and nested objects expressed with braces, brackets, colons, and commas. XML expresses the same hierarchical relationships using opening and closing element tags with text content and attributes. The two formats share the same fundamental data model — tree-structured, hierarchical data — but differ significantly in verbosity, readability, tooling support, and ecosystem conventions. Converting between them is necessary whenever two systems in an integration pipeline use different formats, when migrating from legacy XML-based infrastructure to modern JSON APIs (or vice versa), or when generating XML documents from JSON configuration data in build pipelines and document generation workflows.
How Does the Conversion Work?
The conversion algorithm works recursively through the JSON value tree. A JSON object becomes an XML element whose child elements correspond to each key-value pair in the object. A JSON array becomes a parent element containing one repeated child element per array item, with the child tag name configurable (default: 'item'). A JSON string, number, or boolean becomes a text node inside an element. A null value is handled according to your chosen strategy: omit the element entirely, render it as a self-closing empty element, or write the literal text 'null'. Element tag names are derived from JSON keys and sanitized to comply with XML NCName rules: spaces become underscores, leading digits get an underscore prefix, colons are replaced (they are reserved for XML namespaces), and other invalid characters are stripped. If the XML declaration is enabled, the output begins with <?xml version="1.0" encoding="UTF-8"?>.
Why Does Format Conversion Matter?
Despite JSON's dominance in modern web development, XML is far from obsolete. SOAP web services — still widely used in banking, healthcare (HL7 FHIR has both JSON and XML profiles), government systems, and enterprise middleware — require XML payloads. Configuration standards like Maven POM files, Android manifests, Spring XML configuration, and Microsoft XAML are XML-native. Document formats including Microsoft Office OOXML, OpenDocument Format, and SVG are XML at their core. RSS and Atom feeds are XML. Many ETL (Extract, Transform, Load) pipelines in data engineering accept XML but produce JSON, or vice versa. Having a reliable, configurable converter that handles edge cases — null values, special characters, invalid tag names, arrays at the root level — saves significant debugging time and prevents subtle data corruption in integration pipelines.
Limitations and Edge Cases
JSON to XML conversion is not lossless in all cases. JSON supports duplicate keys in objects (though it is non-standard), while XML element order is significant but key uniqueness is not enforced the same way. JSON numbers can include floating-point values that serialize straightforwardly, but very large integers or scientific notation values may need careful handling in strongly-typed XML schemas. JSON has no concept of XML namespaces, comments, processing instructions, mixed content (text alongside child elements), or XML attributes — the hyphen-prefix convention this tool supports is a workaround convention, not a standard. When converting for use with XML Schema (XSD) validation, the generated element names and structure must match the schema exactly, which may require post-processing. CDATA sections preserve raw strings but some XML parsers treat them identically to escaped text, while others handle them differently in whitespace-sensitive contexts.
How to Use the JSON to XML Converter
Paste or Upload Your JSON
Click the 'Paste JSON' tab and type or paste your JSON directly into the editor. Alternatively, switch to the 'Upload File' tab and select a .json file from your computer (up to 2 MB). You can also click 'Load Sample JSON' to see a working example with nested objects, arrays, special characters, and the hyphen-prefix attribute convention.
Configure Conversion Options
Set the Root Element Name (the outermost XML tag wrapping your data — default is 'root') and the Array Item Element Name (the tag used for each item in a JSON array — default is 'item'). Choose your preferred indentation style: 2 spaces, 4 spaces, tab, or compact/minified for smallest output. Select how null values should be handled, and toggle the XML declaration and CDATA wrapping options to match your target system's requirements.
Review the XML Output
The converter auto-updates as you type. Check the status badge to confirm your JSON is valid. Review the output statistics panel showing element count, line count, and file size. If any of your JSON keys contained characters invalid in XML element names (spaces, leading digits, special characters), a warning will appear listing the keys that were automatically sanitized — verify the sanitized names meet your needs.
Copy or Download the Result
Click 'Copy XML' to copy the entire XML output to your clipboard, ready to paste into your IDE, API client, or document editor. Click 'Download .xml' to save the result as a file named 'converted.xml'. The download uses your browser's built-in file saving — no server upload of your data occurs.
Frequently Asked Questions
Why does my JSON key appear differently in the XML output?
XML element names (NCNames) must follow strict rules: they cannot start with a digit or hyphen, cannot contain spaces, colons, or most special characters, and cannot begin with the string 'xml' (in any case). When your JSON contains keys that violate these rules, the converter automatically sanitizes them — replacing spaces with underscores, prefixing digit-leading names with an underscore, and removing unsupported characters. For example, a key named '1stItem' becomes '_1stItem', and 'first name' becomes 'first_name'. A yellow warning banner lists every key that was changed, so you always know when sanitization occurs and can adjust your source JSON if needed.
What is the hyphen-prefix convention and when should I use it?
The hyphen-prefix convention is a widely used workaround to express XML attributes within a JSON structure. When a JSON object key starts with a hyphen (dash) character, this converter treats that key as an XML attribute on the parent element rather than a child element. For example, { "book": { "-id": "b001", "title": "XML Guide" } } produces <book id="b001"><title>XML Guide</title></book>. Use this convention when you need to generate XML with attributes — common in XML schemas, XSLT stylesheets, SVG, and SOAP envelope formats. It is supported by several XML serialization libraries including xml2js and fast-xml-parser.
What is CDATA wrapping and when should I use it?
CDATA (Character Data) sections are XML blocks that tell parsers to treat the content as literal text rather than markup. They are written as <![CDATA[your text here]]>. When enabled, the converter wraps any string value that contains XML special characters — angle brackets (<, >), ampersand (&), or quotation marks — in a CDATA section instead of escaping them as <, >, or &. Use CDATA when your strings contain HTML snippets, code fragments, or other markup that would be awkward to read or process when escape-encoded. Note that some XML parsers treat CDATA and escaped text identically, while others preserve the CDATA block as-is in their internal representation.
How are JSON arrays converted to XML?
JSON arrays become repeated sibling child elements under a parent element. The parent element's tag name is the JSON key containing the array. Each array item becomes a child element with the tag name you specify in the 'Array Item Element Name' field (default: 'item'). For example, { "colors": ["red", "green"] } becomes <colors><item>red</item><item>green</item></colors>. If the JSON root itself is an array, each top-level item is wrapped in the array item tag and all items are grouped inside the root element you specified. For NDJSON (newline-delimited JSON), each line is treated as a separate top-level object and each becomes a child of the root element.
Is my JSON data sent to a server?
No. All conversion processing happens entirely in your browser using JavaScript. Your JSON data never leaves your device — it is not uploaded to any server, logged, stored, or transmitted over the network. This makes the tool safe to use with sensitive or confidential data such as API responses containing personal information, internal system configurations, or proprietary data structures. The file upload feature also reads the file locally using the browser's FileReader API. The download feature creates the file in memory and saves it directly to your device without any server round-trip.
What is NDJSON and does this tool support it?
NDJSON (Newline Delimited JSON), also called JSONLines, is a format where each line of a text file is a valid, self-contained JSON object. It is commonly used for streaming data, log files, and bulk data exports from databases like MongoDB. Instead of wrapping all records in a single JSON array, each record stands alone on its line, which makes it possible to process very large datasets line by line without loading the entire file into memory. This converter automatically detects NDJSON when multiple lines are present and each line begins with a JSON object or array. Each line is parsed independently and wrapped as a child element under your specified root element name.