XML to JSON: A Guide to Modernizing Legacy System Interface Data
This article is written for developers integrating with legacy systems. It explains the standard mapping rules and special scenario handling for XML to JSON conversion, covering 3 typical business scenarios.
अपडेट 2026-08-11
Standard Mapping Rules from XML Nodes to JSON
XML elements correspond to JSON objects, and text nodes within elements are stored under the #text key by default. All attributes of XML elements are prefixed with @_ as JSON object keys to distinguish them from child elements, a convention adopted by most conversion tools.
Duplicate child elements with the same name are automatically aggregated into a JSON array. Regardless of whether the child element appears 1 time or N times, the output maintains a consistent array structure, eliminating the need for type checking in frontend business logic.
Conversion Handling for Three Typical XML Scenarios
For envelope-style XML returned by legacy backend SOAP interfaces, the core data inside the Body node is extracted and converted to the corresponding JSON structure, with namespace identifiers retained for subsequent processing. After conversion of RSS feed XML, entry nodes are automatically aggregated into an array that can be directly rendered as a list by the frontend. After conversion of legacy project configuration XML, the output can be directly read and used by Node.js backend services.
| Scenario Type | Core Conversion Logic |
|---|---|
| SOAP interface | Extract core content from Body, retain envelope metadata |
| RSS feed | Automatically aggregate item nodes into an array |
| Legacy system configuration file | Retain hierarchical structure for new service reading |
Special Structure Handling: Namespaces and CDATA
XML namespace prefixes are retained in full when converted to corresponding JSON key names, and are not automatically removed. Developers can filter unwanted namespace identifiers according to business requirements. OKfmt's XML to JSON converter retains original namespace information by default.
Text within CDATA sections directly retains its original content, stored as the string value of the corresponding node, with no escaping or truncation applied. This ensures the structural integrity of special text such as HTML fragments and script content.
Flattening Trade-offs for Nested Depth
Native XML supports multi-layer nested structures, which are preserved in the corresponding hierarchy after conversion to JSON, adapting to the nested object processing logic of modern APIs. For business scenarios that require flat storage, nested structures deeper than 3 layers can be manually flattened by path, with underscores used to concatenate parent node key names.
Flattening reduces the complexity of hierarchical access during JSON parsing, making it suitable for storage in relational databases or log analysis. Conversion tools retain the original nested structure by default, and allow users to independently choose whether to enable flattening.
अक्सर पूछे जाने वाले प्रश्न
Does the conversion retain all original content from the XML?
OKfmt's XML to JSON converter retains all XML nodes, attributes, and CDATA content by default, only removing formatting irrelevant blank lines and line breaks. The converted data can be verified to match the information of the original XML.
Why use the @_ prefix to distinguish attributes?
This convention avoids key conflicts when an attribute and a child element share the same name. Most existing open source conversion libraries follow this rule, and downstream code can directly adapt to this naming logic.
What is the pricing for the online XML to JSON converter?
The XML to JSON converter provided by OKfmt is a free web tool. Basic conversion functions have no usage limits. Value-added service rules are subject to the current public notice on the official website.