{"id":394,"date":"2026-10-01T18:00:02","date_gmt":"2026-10-01T18:00:02","guid":{"rendered":"https:\/\/html-editor-online.com\/blog?p=394"},"modified":"2026-10-01T18:00:02","modified_gmt":"2026-10-01T18:00:02","slug":"xml-to-json-single-element-object-multiple-elements-array","status":"publish","type":"post","link":"https:\/\/html-editor-online.com\/blog\/xml-to-json-single-element-object-multiple-elements-array\/","title":{"rendered":"XML to JSON Single Element Object, Multiple Elements Array Explained"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">When you run XML through the site&#8217;s <a href=\"https:\/\/html-editor-online.com\/tools\/xml_to_json\" title=\"XML to JSON\">XML to JSON converter<\/a>, one <code>&lt;item&gt;<\/code> comes out as a JSON object while two come out as an array. That is the XML to JSON single element object, multiple elements array behavior, and it happens because the converter groups repeated sibling elements with the same tag name into an array. A lone element has nothing to group with, so it stays an object.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">This article assumes you know basic XML elements and the difference between JSON objects and arrays. The input is a well-formed XML feed, the output is JSON, and the practical outcome is deciding whether your consuming code must normalize a lone item into an array. I show paired samples, the failure they cause, a small normalization helper, and why an XML check says nothing about the resulting JSON shape.<\/p>\n\n\n\n<div class=\"wp-block-rank-math-toc-block\" id=\"rank-math-toc\"><h2>Table of Contents<\/h2><nav><ul><li class=\"\"><a href=\"#xml-to-json-single-element-object-vs-multiple-elements-array-the-rule-1\">XML to JSON Single Element Object vs Multiple Elements Array: The Rule<\/a><\/li><li class=\"\"><a href=\"#one-item-vs-two-items-paired-xml-and-json-samples-2\">One Item vs Two Items: Paired XML and JSON Samples<\/a><\/li><li class=\"\"><a href=\"#why-the-shape-change-breaks-consumer-code-3\">Why the Shape Change Breaks Consumer Code<\/a><\/li><li class=\"\"><a href=\"#compare-real-outputs-then-normalize-only-if-you-need-an-array-4\">Compare Real Outputs, Then Normalize Only If You Need an Array<\/a><\/li><li class=\"\"><a href=\"#what-xml-validation-does-not-tell-you-about-json-shape-5\">What XML Validation Does Not Tell You About JSON Shape<\/a><\/li><li class=\"\"><a href=\"#edge-cases-and-limits-of-xml-to-json-mapping-6\">Edge Cases and Limits of XML-to-JSON Mapping<\/a><\/li><li class=\"\"><a href=\"#frequently-asked-questions\">Frequently asked questions<\/a><\/li><li class=\"\"><a href=\"#next-steps\">Next steps<\/a><\/li><li class=\"\"><a href=\"#sources\">Sources and further reading<\/a><\/li><\/ul><\/nav><\/div>\n\n\n<div class=\"wp-block-image heo-auto-image\">\n<figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"576\" src=\"https:\/\/html-editor-online.com\/blog\/wp-content\/uploads\/2026\/10\/xml-to-json-single-element-object-multiple-elements-array-image-1-366-1-df433019715398c3-1024x576.jpg\" alt=\"Conceptual illustration of one box versus a group of two matching boxes, representing a single XML element and repeated sibling elements\" class=\"wp-image-397\" srcset=\"https:\/\/html-editor-online.com\/blog\/wp-content\/uploads\/2026\/10\/xml-to-json-single-element-object-multiple-elements-array-image-1-366-1-df433019715398c3-1024x576.jpg 1024w, https:\/\/html-editor-online.com\/blog\/wp-content\/uploads\/2026\/10\/xml-to-json-single-element-object-multiple-elements-array-image-1-366-1-df433019715398c3-300x169.jpg 300w, https:\/\/html-editor-online.com\/blog\/wp-content\/uploads\/2026\/10\/xml-to-json-single-element-object-multiple-elements-array-image-1-366-1-df433019715398c3-768x432.jpg 768w, https:\/\/html-editor-online.com\/blog\/wp-content\/uploads\/2026\/10\/xml-to-json-single-element-object-multiple-elements-array-image-1-366-1-df433019715398c3-747x420.jpg 747w, https:\/\/html-editor-online.com\/blog\/wp-content\/uploads\/2026\/10\/xml-to-json-single-element-object-multiple-elements-array-image-1-366-1-df433019715398c3-150x84.jpg 150w, https:\/\/html-editor-online.com\/blog\/wp-content\/uploads\/2026\/10\/xml-to-json-single-element-object-multiple-elements-array-image-1-366-1-df433019715398c3-696x392.jpg 696w, https:\/\/html-editor-online.com\/blog\/wp-content\/uploads\/2026\/10\/xml-to-json-single-element-object-multiple-elements-array-image-1-366-1-df433019715398c3-1068x601.jpg 1068w, https:\/\/html-editor-online.com\/blog\/wp-content\/uploads\/2026\/10\/xml-to-json-single-element-object-multiple-elements-array-image-1-366-1-df433019715398c3.jpg 1200w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n<\/div>\n\n\n<h2 class=\"wp-block-heading\" id=\"xml-to-json-single-element-object-vs-multiple-elements-array-the-rule-1\">XML to JSON Single Element Object vs Multiple Elements Array: The Rule<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">XML has no array type. A feed with two <code>&lt;item&gt;<\/code> children is simply two sibling elements that share a name, and a feed with one is a single child. JSON forces a choice: a key holds either an object or an array. Every XML-to-JSON converter needs a rule to bridge that gap.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">As of October 1, 2026, the site&#8217;s XML to JSON page says it groups repeated sibling elements with the same tag name into an array. That is the converter&#8217;s mapping rule, not a requirement of XML or JSON. <a href=\"https:\/\/www.rfc-editor.org\/info\/rfc8259\/\" title=\"https:\/\/www.rfc-editor.org\/info\/rfc8259\/\" target=\"_blank\" rel=\"noopener\">RFC 8259<\/a> defines JSON objects and arrays but does not prescribe how XML must map to them, and the <a href=\"https:\/\/www.w3.org\/TR\/xml\/\" title=\"https:\/\/www.w3.org\/TR\/xml\/\" target=\"_blank\" rel=\"noopener\">W3C XML specification<\/a> says nothing about JSON at all.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The consequence follows directly. When the converter sees one <code>item<\/code> under <code>feed<\/code>, there is no repetition, so the value of <code>item<\/code> is the object for that single element. When it sees two, it collects them into an array. The page documents automatic grouping and optional attribute preservation, but it does not document a force-array setting, so I would not assume one exists. I also would not assume every other converter behaves the same way: some libraries always emit arrays, some never do, and some follow a rule like this one.<\/p>\n\n\n<div class=\"wp-block-image heo-auto-image\">\n<figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"576\" src=\"https:\/\/html-editor-online.com\/blog\/wp-content\/uploads\/2026\/10\/xml-to-json-single-element-object-multiple-elements-array-image-2-366-2-8204a28f8dcb260b-1024x576.jpg\" alt=\"Illustration of two display cases, one holding a single item and one holding two, comparing one-item and two-item XML conversions\" class=\"wp-image-399\" srcset=\"https:\/\/html-editor-online.com\/blog\/wp-content\/uploads\/2026\/10\/xml-to-json-single-element-object-multiple-elements-array-image-2-366-2-8204a28f8dcb260b-1024x576.jpg 1024w, https:\/\/html-editor-online.com\/blog\/wp-content\/uploads\/2026\/10\/xml-to-json-single-element-object-multiple-elements-array-image-2-366-2-8204a28f8dcb260b-300x169.jpg 300w, https:\/\/html-editor-online.com\/blog\/wp-content\/uploads\/2026\/10\/xml-to-json-single-element-object-multiple-elements-array-image-2-366-2-8204a28f8dcb260b-768x432.jpg 768w, https:\/\/html-editor-online.com\/blog\/wp-content\/uploads\/2026\/10\/xml-to-json-single-element-object-multiple-elements-array-image-2-366-2-8204a28f8dcb260b-747x420.jpg 747w, https:\/\/html-editor-online.com\/blog\/wp-content\/uploads\/2026\/10\/xml-to-json-single-element-object-multiple-elements-array-image-2-366-2-8204a28f8dcb260b-150x84.jpg 150w, https:\/\/html-editor-online.com\/blog\/wp-content\/uploads\/2026\/10\/xml-to-json-single-element-object-multiple-elements-array-image-2-366-2-8204a28f8dcb260b-696x392.jpg 696w, https:\/\/html-editor-online.com\/blog\/wp-content\/uploads\/2026\/10\/xml-to-json-single-element-object-multiple-elements-array-image-2-366-2-8204a28f8dcb260b-1068x601.jpg 1068w, https:\/\/html-editor-online.com\/blog\/wp-content\/uploads\/2026\/10\/xml-to-json-single-element-object-multiple-elements-array-image-2-366-2-8204a28f8dcb260b.jpg 1200w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n<\/div>\n\n\n<h2 class=\"wp-block-heading\" id=\"one-item-vs-two-items-paired-xml-and-json-samples-2\">One Item vs Two Items: Paired XML and JSON Samples<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Both samples below use a structured item, an <code>item<\/code> that contains an <code>id<\/code> child, so the lone value is visibly an object rather than a plain string. They contain no attributes, which keeps the attribute option out of the picture. If a sample is hard to read, run it through the <a href=\"https:\/\/html-editor-online.com\/tools\/xml_formatter\" title=\"XML Formatter\">XML Formatter<\/a> first so the nesting is easy to see.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The JSON shown follows the rules documented on the site&#8217;s page. I did not independently verify it by running the interactive converter, so paste each sample into the tool yourself and compare the result with what I list here.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"section-1-detail-1\">One item<\/h3>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"xml\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;feed&gt;&lt;item&gt;&lt;id&gt;1&lt;\/id&gt;&lt;\/item&gt;&lt;\/feed&gt;<\/pre>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"json\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">{&quot;feed&quot;:{&quot;item&quot;:{&quot;id&quot;:&quot;1&quot;}}}<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"section-1-detail-2\">Two items<\/h3>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"xml\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;feed&gt;&lt;item&gt;&lt;id&gt;1&lt;\/id&gt;&lt;\/item&gt;&lt;item&gt;&lt;id&gt;2&lt;\/id&gt;&lt;\/item&gt;&lt;\/feed&gt;<\/pre>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"json\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">{&quot;feed&quot;:{&quot;item&quot;:&#091;{&quot;id&quot;:&quot;1&quot;},{&quot;id&quot;:&quot;2&quot;}&#093;}}<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The important difference is the value of <code>item<\/code>. In the first output it begins with <code>{<\/code> and is an object. In the second it begins with <code>&#091;<\/code> and is an array of objects. The key name, the parent and the child values are identical, yet the type of the value changed because the number of siblings changed.<\/p>\n\n\n<div class=\"wp-block-image heo-auto-image\">\n<figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"576\" src=\"https:\/\/html-editor-online.com\/blog\/wp-content\/uploads\/2026\/10\/xml-to-json-single-element-object-multiple-elements-array-image-3-366-3-4dd65a350157d973-1024x576.jpg\" alt=\"Illustration of a single parcel jamming a gate built for groups, representing code that expects an array receiving one object\" class=\"wp-image-401\" srcset=\"https:\/\/html-editor-online.com\/blog\/wp-content\/uploads\/2026\/10\/xml-to-json-single-element-object-multiple-elements-array-image-3-366-3-4dd65a350157d973-1024x576.jpg 1024w, https:\/\/html-editor-online.com\/blog\/wp-content\/uploads\/2026\/10\/xml-to-json-single-element-object-multiple-elements-array-image-3-366-3-4dd65a350157d973-300x169.jpg 300w, https:\/\/html-editor-online.com\/blog\/wp-content\/uploads\/2026\/10\/xml-to-json-single-element-object-multiple-elements-array-image-3-366-3-4dd65a350157d973-768x432.jpg 768w, https:\/\/html-editor-online.com\/blog\/wp-content\/uploads\/2026\/10\/xml-to-json-single-element-object-multiple-elements-array-image-3-366-3-4dd65a350157d973-747x420.jpg 747w, https:\/\/html-editor-online.com\/blog\/wp-content\/uploads\/2026\/10\/xml-to-json-single-element-object-multiple-elements-array-image-3-366-3-4dd65a350157d973-150x84.jpg 150w, https:\/\/html-editor-online.com\/blog\/wp-content\/uploads\/2026\/10\/xml-to-json-single-element-object-multiple-elements-array-image-3-366-3-4dd65a350157d973-696x392.jpg 696w, https:\/\/html-editor-online.com\/blog\/wp-content\/uploads\/2026\/10\/xml-to-json-single-element-object-multiple-elements-array-image-3-366-3-4dd65a350157d973-1068x601.jpg 1068w, https:\/\/html-editor-online.com\/blog\/wp-content\/uploads\/2026\/10\/xml-to-json-single-element-object-multiple-elements-array-image-3-366-3-4dd65a350157d973.jpg 1200w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n<\/div>\n\n\n<h2 class=\"wp-block-heading\" id=\"why-the-shape-change-breaks-consumer-code-3\">Why the Shape Change Breaks Consumer Code<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Both outputs are valid JSON, so nothing in a syntax check flags the difference. The trouble appears in code that assumes a collection. Consider a consumer written against a two-item sample:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"js\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">const payload = JSON.parse(jsonText);\nconst ids = payload.feed.item.map((item) =&gt; item.id);\nconsole.log(ids);<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">With two items, <code>payload.feed.item<\/code> is an array and the code logs both ids. With one item, it is a plain object, and plain objects have no <code>map<\/code> method, so the call throws a TypeError. A <code>for...of<\/code> loop over the lone object fails as well because the object is not iterable, and reading <code>.length<\/code> quietly gives <code>undefined<\/code>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">This kind of bug is intermittent, which makes it annoying. Your fixtures contain two items, the tests pass, and the code breaks on a quiet day when the feed contains exactly one. If an item were text-only, such as <code>&lt;item&gt;1&lt;\/item&gt;<\/code>, a lone value would be a string instead, and strings also lack <code>map<\/code>, so the same assumption fails in a different way.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">If you want to try the snippet against both shapes, the <a href=\"https:\/\/html-editor-online.com\/editors\/javascript\" title=\"JavaScript Editor\">JavaScript Editor<\/a> runs code in the browser and shows synchronous console output. Only run code you wrote yourself, and keep the sample data fictitious.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"compare-real-outputs-then-normalize-only-if-you-need-an-array-4\">Compare Real Outputs, Then Normalize Only If You Need an Array<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Do not change the consumer based on my samples alone. Use your own feed and follow these steps:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Collect a real feed response with one item, one with several, and, if the producer can send it, one with none.<\/li>\n\n\n\n<li>Convert each response and note the type at the path your code reads, here <code>feed.item<\/code>. A <a href=\"https:\/\/html-editor-online.com\/tools\/json_formatter\" title=\"JSON Formatter\">JSON Formatter<\/a> makes the opening <code>{<\/code> or <code>&#091;<\/code> easy to spot.<\/li>\n\n\n\n<li>Decide what your contract is. If the consuming code treats the field as a collection, it needs an array every time.<\/li>\n\n\n\n<li>Normalize at the boundary where the converted JSON enters your code, not in every function that reads it.<\/li>\n\n\n\n<li>Test the normalized result with zero, one and two items.<\/li>\n\n\n<\/ol>\n\n\n\n<p class=\"wp-block-paragraph\">This is the same habit described in the guide on <a href=\"https:\/\/html-editor-online.com\/blog\/json-to-csv-only-one-row\/\" title=\"Why Is My JSON-to-CSV Export One Row Instead of One Row per Record?\">locating the records array before converting JSON to CSV<\/a>: check the actual shape you hold before writing code that depends on it. Here is a small helper:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"js\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">function toArray(value) {\n  if (value == null) {\n    return &#091;&#093;;\n  }\n  return Array.isArray(value) ? value : &#091;value&#093;;\n}\n\nconst payload = JSON.parse(jsonText);\nconst items = toArray(payload.feed?.item);\nconsole.log(items.length, items.map((item) =&gt; item.id));<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Treating a missing value as an empty list is a policy choice, not a rule. If your feed must always contain at least one item, reject the payload instead:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"js\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">const value = payload.feed?.item;\nif (value == null) {\n  throw new Error(&#039;The feed must contain at least one item&#039;);\n}\nconst items = Array.isArray(value) ? value : &#091;value&#093;;<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><code>Array.isArray()<\/code> inspects the runtime value, so it does not guess from the contents. MDN&#8217;s page, last modified July 10, 2025, lists it as widely available across browsers since July 2015, which makes it a safe check for current browsers (<a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/JavaScript\/Reference\/Global_Objects\/Array\/isArray\" title=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/JavaScript\/Reference\/Global_Objects\/Array\/isArray\" target=\"_blank\" rel=\"noopener\">MDN reference<\/a>).<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Also test how your converter treats an empty element such as <code>&lt;item\/&gt;<\/code>. I have not documented its output here, so run it and look before deciding whether <code>toArray<\/code> should treat that case specially.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"what-xml-validation-does-not-tell-you-about-json-shape-5\">What XML Validation Does Not Tell You About JSON Shape<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">It is tempting to think a validated feed guarantees a stable result, but each check answers a different question. The site describes its <a href=\"https:\/\/html-editor-online.com\/tools\/xml_validator\" title=\"XML Validator\">XML Validator<\/a> as detecting parsing errors and malformed tags, which is a well-formedness check. The W3C XML 1.0 specification distinguishes well-formedness from validity, and validity against a DTD or schema is a stronger, separate check.<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>Check<\/th><th>What it tells you<\/th><th>What it does not tell you<\/th><\/tr><\/thead><tbody><tr><td>XML well-formedness<\/td><td>Tags are balanced and the document parses<\/td><td>Whether the data matches a schema, or how it maps to JSON<\/td><\/tr><tr><td>DTD or XSD validity<\/td><td>The XML follows declared structure rules<\/td><td>Which JSON shape a particular converter emits<\/td><\/tr><tr><td>JSON syntax check<\/td><td>The output parses as JSON<\/td><td>Whether a field is an object or an array in every case<\/td><\/tr><tr><td>Runtime test of your code<\/td><td>Your code handles the samples you tried<\/td><td>Inputs you did not try<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">Even an occurrence constraint in an XML schema, such as allowing one or more items, describes the XML. It does not dictate that a JSON converter will emit an array for a single occurrence. The converter&#8217;s grouping rule decides that, which is why the output comparison in the previous section matters.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The <a href=\"https:\/\/html-editor-online.com\/tools\/json_validator\" title=\"JSON Validator\">JSON Validator<\/a> checks syntax with the browser&#8217;s <code>JSON.parse<\/code>, and both <code>{\"id\":\"1\"}<\/code> and <code>&#091;{\"id\":\"1\"}&#093;<\/code> pass. Use these tools for what they do, then protect the shape with the boundary normalization and tests described above.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"edge-cases-and-limits-of-xml-to-json-mapping-6\">Edge Cases and Limits of XML-to-JSON Mapping<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">I kept attributes and mixed content out of the introductory examples on purpose. The site&#8217;s documentation describes optional <code>@attributes<\/code> and <code>#text<\/code> conventions, and enabling attribute preservation changes what an element looks like in JSON. If your feed uses attributes, run one-item and two-item samples with the same options you will use in production, and compare them before writing code.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Other limits apply to any XML-to-JSON mapping. It is not lossless: element ordering across different tag names, namespace prefixes and mixed text-and-element content can all be flattened or reshaped. Do not promise yourself that another converter, or a future version of this one, will produce the same shape. If the feed is something you depend on, ask the producer for a written contract and keep a few real sample files as regression fixtures.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">When you need to go the other direction, the <a href=\"https:\/\/html-editor-online.com\/tools\/json_to_xml\" title=\"JSON to XML\">JSON to XML<\/a> tool produces well-formed XML with a custom root element. Do not assume a round trip reproduces your original document; compare it against the source. For hand-editing sample output, the <a href=\"https:\/\/html-editor-online.com\/editors\/json\" title=\"JSON Editor\">JSON Editor<\/a> offers syntax highlighting, folding and <code>JSON.parse<\/code> syntax validation, but no tree view or JSON Schema validation, so shape checks still belong in your code and tests.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"frequently-asked-questions\">Frequently asked questions<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"faq-1\">Is it a bug that a single XML item becomes a JSON object?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">No. XML has no array type, so the converter needs a rule. The site&#8217;s XML to JSON page says it groups repeated sibling elements with the same tag name into an array, which means a lone element has nothing to group and stays an object. The behavior is a mapping choice, not an error.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"faq-2\">Does the site&#039;s XML to JSON converter have a force-array option?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">The published page documents automatic grouping of repeated elements and optional attribute preservation. It does not document a force-array setting, so I would not rely on one. Normalize in your consuming code instead.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"faq-3\">Should I always wrap the value in an array?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Only if your code treats the field as a collection. A helper that returns the value if it is already an array and wraps it otherwise is a common choice. Decide separately whether a missing value means an empty list or an error, because that depends on what your feed promises.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"faq-4\">Can the XML Validator tell me whether item will be an array?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">No. It detects parsing errors and malformed tags, which is a well-formedness check. It does not examine the JSON that a converter will later produce, so you still need to compare actual outputs.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"faq-5\">Would a DTD or XSD solve the problem?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">A schema describes allowed structure in the XML, including how often an element may occur. It does not control the converter&#8217;s JSON mapping, so a single permitted occurrence can still convert to an object. The converter&#8217;s grouping rule decides the shape.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"faq-6\">Is Array.isArray safe to use in browsers?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">MDN&#8217;s page, last modified July 10, 2025, identifies Array.isArray as widely available across browsers since July 2015. It checks the runtime value directly, which is why it is better than guessing from the value&#8217;s contents.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"faq-7\">How do attributes change the one-item versus two-item behavior?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">The site documents optional attribute preservation using @attributes and #text conventions, which changes the contents of each element&#8217;s JSON. The sibling grouping rule is described separately, so test both shapes with the same options you will use in production rather than assuming how the two interact.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"next-steps\">Next steps<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">The XML to JSON single element object, multiple elements array difference is a mapping consequence, not a bug in your feed: repeated siblings become an array, and a lone element does not. Compare real one-item and two-item outputs, normalize at the boundary only if your code needs a collection, and decide deliberately what an empty result means. As a next step, paste a one-item and a two-item sample from your own feed into the converter, then add a test for each shape next to your <code>Array.isArray<\/code> check.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Related tools and resources on HTML Editor Online:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/html-editor-online.com\/tools\/xml_to_json\">XML to JSON<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/html-editor-online.com\/tools\/xml_validator\">XML Validator<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/html-editor-online.com\/tools\/json_to_xml\">JSON to XML<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/html-editor-online.com\/blog\/json-to-csv-only-one-row\/\">Why Is My JSON-to-CSV Export One Row Instead of One Row per Record?<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/html-editor-online.com\/tools\/json_formatter\">JSON Formatter<\/a><\/li>\n\n\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"sources\">Sources and further reading<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/www.w3.org\/TR\/xml\/\" target=\"_blank\" rel=\"noopener\">https:\/\/www.w3.org\/TR\/xml\/<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/www.rfc-editor.org\/info\/rfc8259\/\" target=\"_blank\" rel=\"noopener\">https:\/\/www.rfc-editor.org\/info\/rfc8259\/<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/JavaScript\/Reference\/Global_Objects\/Array\/isArray\" target=\"_blank\" rel=\"noopener\">https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/JavaScript\/Reference\/Global_Objects\/Array\/isArray<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/html-editor-online.com\/tools\/xml_to_json\">https:\/\/html-editor-online.com\/tools\/xml_to_json<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/html-editor-online.com\/\">https:\/\/html-editor-online.com\/<\/a><\/li>\n\n\n<\/ul>\n\n\n","protected":false},"excerpt":{"rendered":"<p>One XML item converts to a JSON object, while two items convert to an array. Learn the mapping rule, the bug it causes in consuming code, and how to normalize safely.<\/p>\n","protected":false},"author":1,"featured_media":395,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[27],"tags":[101,99,100,50,98,97,102],"class_list":["post-394","post","type-post","status-publish","format-standard","has-post-thumbnail","category-json-tools","tag-api-consumers","tag-array-isarray","tag-data-normalization","tag-json-arrays","tag-xml-feeds","tag-xml-to-json","tag-xml-well-formedness"],"_links":{"self":[{"href":"https:\/\/html-editor-online.com\/blog\/wp-json\/wp\/v2\/posts\/394","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/html-editor-online.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/html-editor-online.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/html-editor-online.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/html-editor-online.com\/blog\/wp-json\/wp\/v2\/comments?post=394"}],"version-history":[{"count":4,"href":"https:\/\/html-editor-online.com\/blog\/wp-json\/wp\/v2\/posts\/394\/revisions"}],"predecessor-version":[{"id":402,"href":"https:\/\/html-editor-online.com\/blog\/wp-json\/wp\/v2\/posts\/394\/revisions\/402"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/html-editor-online.com\/blog\/wp-json\/wp\/v2\/media\/395"}],"wp:attachment":[{"href":"https:\/\/html-editor-online.com\/blog\/wp-json\/wp\/v2\/media?parent=394"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/html-editor-online.com\/blog\/wp-json\/wp\/v2\/categories?post=394"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/html-editor-online.com\/blog\/wp-json\/wp\/v2\/tags?post=394"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}