XML to JSON Converter
Preparing workspace…
An XML to JSON converter parses an XML document into structured JSON by turning elements into object properties, attributes into marked keys, and repeated sibling elements into arrays. Paste or upload well-formed XML, select whether the output should retain the outer root element as a property, and choose how consistently child values should be wrapped in arrays. The tool keeps attributes under @-prefixed names and places mixed text in #text where an element also contains attributes or children. It explicitly rejects DTD and entity declarations and reports malformed XML rather than guessing how to repair it. XML supports concepts that JSON cannot reproduce exactly, including namespace binding, sibling order among unlike tags, comments and mixed-content position. This conversion is therefore intended for inspecting and exchanging ordinary structured records, not as a byte-perfect round trip for every XML vocabulary.
XML To JSON Converter At a Glance
| Factor | Details |
|---|---|
| Input | XML document up to 4 MiB |
| Parsing | Well-formed XML; DTDs prohibited |
| Attributes | @attribute keys |
| Repeated siblings | Arrays when multiple; optional always-array |
| Text content | #text for mixed object/text nodes |
| Export | Nested UTF-8 JSON with structure counts |
How XML Element Trees Map into JSON
XML documents are hierarchical trees whose nodes have names, attributes and ordered children. JSON objects can also contain nested values, but they do not distinguish an element from its attributes without an added convention. The converter reads the XML root, visits each element and assigns child elements to properties by their tag name. When a child has only text content, its property may be a simple JSON string. When the child also has attributes or nested elements, the property becomes an object. This makes ordinary configuration documents and record feeds easier to inspect in JSON-based developer tools. The representation is a policy choice rather than an intrinsic XML standard, so another converter might legitimately produce a different shape from the same source. Check the target application's expected mapping before using the result as a replacement for XML.
Preserving XML Attributes with Named Properties
In XML, an attribute is carried in an opening tag, such as an id on a book element. If you converted both that id and a nested id child into an ordinary JSON property called id, the two would collide. This tool avoids the collision by prefixing attribute keys with @. An element such as book id="7" containing a title then becomes a book object with @id and title fields. Attribute values are always strings in the XML syntax even when they look numeric or boolean. The converter does not silently convert 7 into a number or true into a JSON boolean, because a leading zero or an encoded identifier can be significant. If your application has an XML schema that declares attribute types, perform that type conversion as a separate, explicit step after checking representative records.
Repeated Child Elements and Array Conventions
XML often expresses lists by repeating the same child tag. Three book children under catalog are represented as an array of book records, while a single book normally becomes one value. This produces compact JSON for documents with singular elements, but it means the type of a property can change as its cardinality changes from one to several. The optional always-array setting keeps children inside arrays even when only one occurs. That is useful for API clients that expect a stable collection shape and do not want special handling for a single record. The setting can make ordinary singleton fields more verbose, so choose it according to the consumer's actual data model. Empty elements and text-only children are still represented as values within the same mapping convention.
Text Nodes, Whitespace and Mixed Content
An element with only plain text can be represented directly as a JSON string. If that element also has an attribute or nested children, its own text is placed in a special #text property so it is not lost. The trim-boundary setting removes surrounding whitespace from element text, which is convenient for indented record-oriented XML. It is less suitable for documents where whitespace is intentional, such as poetry, preformatted content or mixed inline markup. Consider a sentence that contains bold text between two ordinary words: XML can preserve the exact sequence of text nodes and formatting elements, while a basic object representation does not retain their interleaving. This converter concatenates direct text nodes and records child elements separately, so mixed-content order is not guaranteed. Use an XML-native parser and node model for those publishing requirements.
Rejecting DTDs and External Entity Declarations
Some XML documents declare a DTD that defines additional entities or validation rules. Entity expansion and remote entity loading have historically created security problems in poorly configured parsers, including disclosure of local files and denial-of-service through expansion. This workspace refuses DOCTYPE and ENTITY declarations instead of relying on a parser to make them harmless. Standard built-in escapes such as & remain supported in ordinary XML text. The document is parsed as XML using the browser's XML parser, and malformed tags or undefined references produce a conversion error. A rejected file is not evidence that it is malicious; it may simply rely on a legacy document type. Convert or sanitize that file in a trusted XML processing environment before using this smaller utility.
Worked Example: Catalog with Two Records
Take a catalog root containing two book elements, each with an id attribute and a title child. The first title contains an escaped ampersand. Parsing produces a JSON object with catalog at the top, then a book array because the sibling name repeats. Each array item has an @id string and a title string. The entity & becomes a literal ampersand in the JSON value because the XML parser resolves normal character references. With always-array enabled, even each title value may appear as a one-element array. This can be helpful when a client expects every child name to represent a collection. Verify the output against a second catalog containing only one book, because cardinality changes are a common source of subtle integration bugs.
XML Namespaces and Qualified Element Names
XML namespaces distinguish vocabulary names that might otherwise collide. A tag such as media:credit may refer to a namespace identifier defined in an xmlns declaration, while an unprefixed credit tag may represent a different concept. The converter retains the node's qualified name as encountered and includes namespace declarations as attributes when the browser exposes them. It does not normalize those names to expanded namespace URIs or create a schema-aware object model. This means a prefix changed by an upstream publisher can alter the resulting JSON property name even when the namespace URI is logically unchanged. For XML formats with several namespaces, prefer a mapping that uses namespace URIs rather than textual prefixes. Test documents with both prefixed and unprefixed elements before designing production database fields around the converter's output.
Element Ordering and Information That May Be Lost
Unlike a JSON array, a JSON object is usually treated as a dictionary of named properties. XML preserves the order of all child elements, including different tag names that alternate in a meaningful sequence. When this converter groups repeated names into arrays, it retains order within each group but cannot faithfully reconstruct a mixed sequence like note, link, note, link from the resulting object alone. Comments and processing instructions are not part of the default output. Empty tags become empty strings, which may differ from a business schema's representation of an absent field. These are structural limits, not parsing failures. If the receiving application depends on document order, preserve child nodes as an ordered list with explicit tag names in a custom mapping rather than relying on grouped JSON properties.
Validating Well-Formedness and Size Bounds
The XML parser checks that element tags close correctly, attributes use valid quoting, and character references are defined. A syntactically valid document can still violate an XSD or another schema that restricts required fields, choices, number formats or element sequences. The tool does not claim to perform that kind of external schema validation. It also enforces limits on input bytes, element count, nesting and output size to reduce memory exhaustion on difficult documents. If a document is rejected for depth or size, reduce the sample to the specific branch you want to investigate, or switch to a streaming parser designed for larger workloads. Do not divide a document by cutting it at arbitrary tags; preserve one well-formed outer element for each sample.
Inspecting the JSON Result Before Reuse
The output uses indented JSON so nested objects and repeated arrays can be reviewed without specialized XML tooling. Check the top-level root setting, sample attributes, text nodes and array shapes against the receiving application's assumptions. Download the file and parse it independently to confirm the generated result is valid JSON after any later manual edits. If you need to reconstruct XML from a modified JSON record, the JSON to XML converter uses @ keys and #text conventions, but you should not expect comments, mixed ordering or namespace logic to survive an arbitrary round trip. Those features require richer source annotations than ordinary JSON contains. Keep the original XML as the authoritative document when exact document fidelity matters.
When XML Should Stay XML
Not every document benefits from JSON conversion. Signed XML, structured publishing files and standards that rely on canonicalization may become invalid after a representation change. Some industry feeds depend on schema-defined element order or namespace details that a JSON client does not know how to preserve. A XML formatter and validator is appropriate when the main objective is examining syntax and readability while keeping the original XML vocabulary. Use conversion when you need to explore record structures, adapt straightforward feeds or prepare compatible data for a known JSON-consuming system. For business-critical exchange, develop test fixtures for empty elements, repeat counts, attributes, encodings and namespaced records, then run integration tests against the actual receiver.
Frequently Asked Questions
How are XML attributes represented?
Attributes become keys with an @ prefix, keeping them distinct from same-named child elements.
What happens when two children have the same name?
The converter groups their values into an array. An always-array option also wraps singleton child values.
Does it support external XML entities?
No. DTD and ENTITY declarations are rejected to avoid unsafe or unpredictable expansion.
Can it reconstruct the exact original XML afterward?
Not generally. Comments, mixed-child ordering, namespace subtleties and formatting may be lost.
Are XML numbers converted to JSON numbers?
No. Text and attribute values remain strings unless a separate schema-aware conversion is performed.
Conclusion
The useful result of XML conversion is an inspectable representation with clear conventions, not an illusion of identical formats. Check repeated fields, attributes, namespaces and mixed text before treating exported JSON as an authoritative replacement.