JSON to XML & XML to JSON Converter
What This Tool Does
This converter translates JSON documents into well-formed XML and parses XML back into JSON. It preserves attributes (prefixed with @), text content (stored as #text), nested structures, and arrays (as repeated elements). Useful for integrating modern JSON APIs with legacy SOAP or XML-based systems.
Inputs Explained
- Direction: JSON → XML or XML → JSON.
- Input: Paste JSON or XML document.
- Root Element: Wraps output XML when converting JSON (default: root).
- Indent: Pretty-print output with 2 or 4 spaces, or minified.
How It Works
JSON to XML: recursively walks the JSON tree, emitting opening/closing tags for each key, with arrays producing repeated elements. XML to JSON: parses XML with DOMParser, walking elements into a JSON tree where attributes become @attr properties and text content becomes #text.
Formula / Logic Used
Convert between JSON and XML with full attribute and nested structure support.
Step-by-Step Example
Input JSON:
{"book":{"@id":"1","title":"Guide","author":"Ramesh"}}
Output XML:
<?xml version="1.0" encoding="UTF-8"?> <book id="1"> <title>Guide</title> <author>Ramesh</author> </book>
Use Cases
- SOAP API integration: Convert JSON request bodies to XML for legacy SOAP services.
- RSS/Atom feed generation: Transform JSON article data into RSS XML format.
- Android/iOS resources: Convert between JSON config and XML resource files.
- Enterprise system migration: Bridge JSON-based modern APIs with XML-heavy enterprise platforms.
- Data exchange: Transform data between partners using different serialization formats.
Assumptions and Limitations
- Attributes are prefixed with @ in JSON representation (common convention).
- Text content alongside elements is stored under #text key to preserve mixed content.
- XML comments, CDATA sections, and processing instructions are not preserved in JSON conversion.
- Namespaced XML is partially supported — namespace prefixes appear in element names but xmlns declarations need manual handling.
Frequently Asked Questions
1: How do I convert JSON to a valid XML schema (XSD)?
Converting JSON directly into XSD is not a simple one-click format change, because XSD describes rules, not just data. First convert a representative JSON sample into XML. Then infer the structure: element names, data types, required fields, repeated arrays, and allowed nesting. From there, generate an XSD and review it manually. A sample can show what exists, but it cannot always prove what is required or optional. For production, treat generated XSD as a starting draft, then refine it with real business validation rules.
2: Can I convert JSON to XML without losing data?
You can preserve most data if you choose a clear mapping before conversion. JSON has objects, arrays, strings, numbers, booleans, and null. XML has elements, attributes, text, namespaces, and order. Problems happen when a converter guesses how to represent arrays, nulls, attributes, or mixed content. For best results, keep a consistent structure, avoid duplicate meanings, and decide whether object properties should become XML elements or attributes. After conversion, test a round trip back to JSON and compare the result, especially for empty values and repeated items.
3: How to convert nested XML elements to JSON arrays?
Nested XML becomes a JSON array when the same child element repeats under the same parent. For example, multiple item elements under order should become "items": [{...}, {...}]. If there is only one item, some converters output an object instead of an array, which can surprise your code. The safer approach is to configure known repeatable element names as arrays, even when only one appears. For APIs, keep this rule consistent so the client never has to guess whether a field is an object or a list.
4: How do I handle XML CDATA sections in JSON?
CDATA is mainly an XML convenience for storing text that contains characters like <, >, or & without escaping every symbol. When converting to JSON, treat CDATA content as a normal string value. The CDATA wrapper itself usually does not need to be preserved unless your application specifically cares about original XML formatting. Be careful not to parse CDATA text as markup by accident. If the CDATA contains HTML, JavaScript, or JSON text, store it as a string and let the receiving application decide how to process it safely.
5: How do I convert XML with mixed content to JSON?
Mixed content means an XML element contains both text and child elements, such as "Hello <b>world</b> today." JSON does not have a built-in mixed-content model, so you need a convention. A common approach is to use fields like "_text" for text and named properties for child elements. Another option is to preserve the inner XML as a single string. For documents, articles, and rich text, preserving inner XML may be cleaner. For data APIs, avoid mixed content where possible because it makes JSON harder to read and validate.
6: Can I convert XML attributes and elements separately to JSON?
Yes, and it is usually a good idea. Attributes and elements can have different meanings in XML, so your JSON should make that difference visible. Many converters prefix attributes with @, store text as #text, or place attributes inside a special object such as "_attributes". For example, <user id="7">Asha</user> might become {"user":{"@id":"7","#text":"Asha"}}. Pick one convention and use it everywhere. This prevents an attribute named id from being confused with a child element also named id.
7: Why does my XML to JSON conversion produce arrays sometimes and objects other times?
This happens because XML does not explicitly say "this element is always a list." A converter only sees what is in the current document. If it finds one item element, it may output an object. If it finds two item elements, it outputs an array. That inconsistency can break code. The fix is to configure repeatable element names, such as item, row, product, or address, to always become arrays. When designing XML for conversion, include clear wrapper elements like items around repeated children.
8: How do I round-trip JSON XML JSON without data loss?
To round-trip safely, use a strict mapping and avoid features that do not match cleanly between formats. Decide how arrays, null values, booleans, numbers, attributes, namespaces, and text nodes will be represented. Keep property names valid as XML element names, because JSON keys can contain characters that XML names cannot. Before trusting the process, convert JSON to XML, convert it back, and compare with the original using a structural diff instead of just looking at formatting. For critical systems, save the original JSON as the source of truth.
9: How do I convert XML with self-closing tags to JSON?
A self-closing tag, such as <middleName/>, represents an empty element. In JSON, that can be mapped as an empty string, null, true-like presence marker, or an empty object, depending on your convention. For data exchange, I usually recommend null when the value is intentionally missing, and an empty string when the field exists but has no text. The important thing is consistency. If <middleName/> sometimes becomes "" and sometimes becomes null, your API users will spend extra time handling edge cases.
10: Why is my JSON-to-XML output invalid XML?
Invalid XML usually comes from JSON keys or values that do not fit XML rules. XML element names cannot start with many symbols, contain spaces in the wrong places, or use some reserved patterns. Text values must escape characters like &, <, and > unless they are wrapped safely. Another common issue is multiple top-level JSON properties becoming multiple XML root elements, while XML needs one root element. Use a root wrapper, sanitize field names, escape text correctly, and validate the output before sending it to another system.
Sources and References
- W3C — XML Specification — Official XML 1.0 standard.
- MDN — DOMParser — Browser API used for XML parsing.
- ECMA-404 — JSON Standard — JSON format specification.
- Badgerfish XML-to-JSON Convention — The JSON-XML mapping convention used in this tool.