XML to JSON
Convert XML files to structured JSON while preserving nested elements, repeated nodes, attributes, text values and the original root element.
Continue with These Tools
Finished using XML to JSON? Here are some related tools that people commonly use next to complete their workflow faster.
JSON to XML
Convert JSON files to well-formed XML with support for nested objects, arrays, booleans, null values, safe XML element names and formatted UTF-8 output.
XML to CSV
Convert XML files to CSV by detecting repeating records, flattening nested elements and attributes, preserving repeated values, and choosing the output delimiter.
XML to Excel
Convert XML files to Excel XLSX spreadsheets by detecting repeated records, flattening nested elements and attributes, and preserving structured data safely.
CSV to JSON
Convert CSV files to JSON with automatic delimiter detection, UTF-8 handling, duplicate-header protection, streaming conversion and optional pretty formatting.
CSV to XML
Convert CSV files to well-formed XML with automatic delimiter detection, configurable root and record elements, safe XML field names and streaming conversion.
JSON Formatter & Validator
Format, beautify, minify and validate JSON online instantly. Detect invalid JSON, organise nested data and create clean, readable JSON directly in your browser.
XML Formatter & Validator
Format, minify and validate XML instantly. Beautify XML with configurable indentation, detect malformed markup and inspect document statistics directly in your browser.
How to Convert XML to JSON
Upload an XML file and convert its hierarchy into structured JSON. SwiftVecto preserves nested elements, repeated sibling elements, attributes, direct text values and the original XML root element while generating UTF-8 JSON.
Overview
The SwiftVecto XML to JSON converter transforms hierarchical XML into structured JSON without flattening the source document. XML elements become JSON properties, repeated sibling elements become arrays, attributes are stored under @attributes, and direct text content is preserved. The original root element remains in the JSON output, making the converted structure easier to understand and more suitable for round-trip data workflows.
Benefits
How It Works
Upload one well-formed XML file.
SwiftVecto validates the XML before conversion.
DOCTYPE and ENTITY declarations are rejected.
The root XML element becomes the top-level JSON property.
Nested XML elements become nested JSON properties.
Repeated sibling elements are grouped into JSON arrays.
XML attributes are stored under @attributes.
Direct element text is preserved as the element value where possible.
When an element contains attributes or children as well as text, the text is stored under #text.
The converted structure is encoded as UTF-8 JSON.
Download the generated JSON file.
How to Use This Tool
-
1Choose or drop one XML file.
-
2Choose whether the JSON should be pretty formatted.
-
3Start the XML to JSON conversion.
-
4Wait while SwiftVecto validates and converts the XML structure.
-
5Download the generated JSON file.
Helpful Tips
- Use well-formed XML for reliable conversion.
- Repeated sibling elements naturally become JSON arrays.
- XML attributes are kept under @attributes instead of being discarded.
- Direct text may appear under #text when the same XML element also contains attributes or nested elements.
- Values are preserved as strings so identifiers such as 001245 remain unchanged.
- Pretty formatting is useful when people need to inspect the JSON.
- Compact JSON can reduce file size for machine-to-machine use.
- DOCTYPE and ENTITY declarations are intentionally rejected for security.
- Review highly complex mixed-content XML if another system requires a strict JSON structure.
Common Uses
Convert an XML API response into JSON.
Convert customer XML records into structured JSON.
Convert product XML containing attributes into JSON.
Convert repeated XML phone elements into a JSON array.
Convert nested XML address structures into nested JSON objects.
Convert XML feeds into JSON for development workflows.
Worked Examples
The following examples demonstrate how this tool can be used in realistic scenarios.
Nested XML
A customer element containing an address child becomes a customer JSON object containing a nested address object.
Repeated elements
Multiple sibling <phone> elements are grouped into a JSON array rather than overwriting one another.
XML attributes
An element such as <customer id="1001"> stores id inside an @attributes JSON object.
Attribute and text
An element such as <price currency="GBP">12.50</price> becomes an object containing @attributes and #text.
Common Mistakes
Avoid these common mistakes to achieve the most accurate results.
- Uploading malformed XML.
- Uploading JSON, CSV or HTML renamed to .xml.
- Expecting every XML value to be automatically converted to JSON numbers or booleans.
- Assuming XML attributes will become ordinary sibling properties.
- Expecting XML mixed content to have one universal JSON representation.
- Including DOCTYPE or ENTITY declarations.
- Assuming namespace-heavy XML will always map exactly to the structure expected by another application.
Glossary
Definitions of the most important terms used by this tool.
XML
Extensible Markup Language, a hierarchical text format that represents data using elements and attributes.
JSON
JavaScript Object Notation, a structured text format based on objects, arrays and scalar values.
XML Attribute
Metadata attached to an XML element, such as id="1001".
@attributes
The JSON property used by SwiftVecto to preserve XML element attributes.
#text
The JSON property used when an XML element contains direct text together with attributes or child elements.
Repeated Sibling
Two or more XML elements with the same name under the same parent, converted into a JSON array.
Root Element
The single outermost XML element, retained as the top-level JSON key.
XMLReader
A PHP XML parser used to process XML while restricting unsafe network entity behaviour.
Frequently Asked Questions
Can I convert XML to JSON?
Yes. SwiftVecto converts well-formed XML into structured UTF-8 JSON.
Does the converter preserve nested XML?
Yes. Nested XML elements become nested JSON objects.
What happens to repeated XML elements?
Repeated sibling elements are converted into JSON arrays.
Are XML attributes preserved?
Yes. Attributes are stored inside an @attributes object.
What does #text mean?
The #text property stores direct text content when the XML element also contains attributes or nested child elements.
Is the XML root element preserved?
Yes. The original root element becomes the top-level property in the generated JSON.
What happens to empty XML elements?
Empty scalar elements are represented as empty strings.
Are numbers converted to JSON numbers?
No automatic type guessing is performed. XML text values are preserved as strings.
Are true and false converted to booleans?
No. XML text does not provide reliable type metadata by itself, so values such as true and false remain strings.
Are leading zeros preserved?
Yes. Values such as 001245 remain strings and keep their leading zeros.
Can I pretty-format the JSON?
Yes. Pretty formatting adds indentation and line breaks without changing the underlying converted structure.
Why are DOCTYPE and ENTITY blocked?
They are not required for normal XML-to-JSON conversion and external entity processing can create security risks.
Does it support XML namespaces?
Namespaced XML can be parsed, but the converter primarily uses local element and attribute names in the generated JSON structure.
Does SwiftVecto change my XML file?
No. The source XML is copied into an isolated processing workspace and remains unchanged.
Things to Know
- One XML file is processed per conversion.
- The XML source must be well formed.
- DOCTYPE and ENTITY declarations are rejected.
- External network entity resolution is disabled.
- The original XML root element is retained.
- Nested elements remain nested in JSON.
- Repeated sibling elements become arrays.
- Attributes are stored under @attributes.
- Direct text may be stored under #text when required.
- Values are preserved as strings.
- Generated JSON uses UTF-8.
- Pretty formatting can be enabled or disabled.
Disclaimer
XML and JSON have different data models, so there is no single universal mapping for attributes, mixed content, namespaces and repeated structures.
The generated JSON is a consistent general-purpose representation and may require further transformation when another system expects a specific schema.
Values are preserved as strings unless explicit type metadata is available and specialised conversion is implemented.
DOCTYPE and ENTITY declarations are not processed.
Users should review important converted data before relying on it for financial, legal, operational or other critical integrations.