Conversion is fast, and that is exactly why it deserves a check. When an XML file becomes JSON, the shape changes by design: elements become keys, repeats become arrays, attributes change their names. In the middle of that legitimate change, a real loss can hide: a dropped element, a misplaced value, a repeat that collapsed. Trusting tidy-looking output is how bad data gets downstream.

This guide teaches a manual structural check for any conversion from Ovatly's XML to JSON Converter: compare counts, array lengths, attribute keys and text placement between the XML and the JSON, and spot-check values until the two agree.

Contents

  • Quick answer
  • What validation means here, and what it does not
  • The mapping rules you are checking against
  • The structural check, step by step
  • Worked example: one product record, checked point by point
  • Where the shapes legitimately differ
  • Limitations and honest tradeoffs
  • Common mistakes
  • FAQs
  • Conclusion
  • References

Quick answer

Validate converted JSON by comparing structure, not by reading it and nodding. Count the child elements of a record in the XML and the keys of the matching object in the JSON. Confirm every repeated element became an array of the same length, every attribute appears as an @ key, and text beside attributes landed in #text. Then spot-check values character by character on the first record, the last record and any odd ones. The XML to JSON Converter reports well-formedness errors in plain words, but schema validation is not part of the conversion: the shape check is yours to run.

What validation means here, and what it does not

Validation in this guide means one thing: the JSON faithfully represents the XML you fed in. Every element, attribute and text value has a predictable home in the output, and your job is to confirm a sample of each kind arrived where the rules say.

It does not mean schema validation. Nothing here checks the document against a schema or a contract, and the converter does not do that either. It also does not mean the data is correct: a wrong price in the XML becomes a faithfully wrong price in the JSON. The one automatic check you do get is well-formedness: if the XML is malformed, the converter reports the problem in plain words instead of guessing.

The mapping rules you are checking against

A check needs a standard, and here the standard is the converter's small set of mapping rules. An element with only text becomes that text. Repeated elements become an array. Attributes become keys prefixed with @. Text that sits beside attributes or child elements goes into a #text key. A single occurrence stays single. Comments, processing instructions and namespaces are not carried over.

Every point in the worked example below is one of these rules applied. When the output breaks a rule, you have found a real problem. When it follows a rule you did not expect, the gap is in your mental model, which is more common.

The structural check, step by step

  1. Pick your records: the first, the last, and any you know is unusual, such as one with a single occurrence of a usually repeated element or one carrying attributes.
  2. Count. In the XML record, count the distinct child element names and the attributes. In the JSON object, count the keys. Each attribute adds an @ key and text beside attributes adds #text, so JSON keys should equal elements plus attributes, plus one when #text is present.
  3. Match the repeats. For every element that appears more than once, find the JSON array and compare its length with the number of occurrences.
  4. Check the attributes. Confirm each attribute appears as an @ prefixed key with the same value.
  5. Check the text. Text-only elements should equal their text exactly. Where an element mixes text with attributes or children, confirm the text sits in #text.
  6. Spot-check values. Compare a handful of values character by character, including one number (it will be a string), one value with punctuation or spaces, and one long value.

Worked example: one product record, checked point by point

Here is the XML record going into the XML to JSON Converter:

<product id="p-1042" currency="USD">
  <name>Trail Shoes</name>
  <price>89.99</price>
  <tag>new</tag>
  <tag>sale</tag>
</product>

And the JSON that comes out:

{
  "product": {
    "@id": "p-1042",
    "@currency": "USD",
    "name": "Trail Shoes",
    "price": "89.99",
    "tag": ["new", "sale"]
  }
}

Now the check. Count first: the XML record has three child element names (name, price, tag) and two attributes (id, currency), so the JSON object should carry five keys. It does: @id, @currency, name, price, tag. Repeats next: tag appears twice in the XML, and the JSON array holds exactly two items, in order. Attributes: both appear under @ keys with their values unchanged. Text: name holds only text and became the string itself. Values: the price reads 89.99 in both, as a string in the JSON, which is the rule, not a defect.

One more shape worth seeing before it surprises you. Give price an attribute of its own:

<price currency="USD">89.99</price>

The text now sits beside an attribute, so it moves into #text:

{ "price": { "@currency": "USD", "#text": "89.99" } }

If you went looking for the price as a plain string, you would call it missing. It is not; it followed the rule.

Where the shapes legitimately differ

Two differences alarm people without indicating any loss. The first is the single-occurrence rule: an element that repeats in most records but appears once in one record becomes an array in the first group and a plain value in the second. Both outputs are correct, and code consuming the JSON has to allow for both shapes or normalise them.

The second is typing: numbers, dates and booleans all arrive as the text they were written as. A checker comparing types instead of characters will flag every number in the file. Compare values as text here, and let your own code apply types afterwards.

Limitations and honest tradeoffs

A manual check samples; it does not prove every record. On a file with ten thousand records, it gives you confidence in the conversion's behaviour, not a guarantee about record 9,412. For full assurance, script the same comparisons in your own tools after conversion, using this method as the specification.

The check also cannot recover what the conversion never carried: comments, processing instructions and namespaces are absent from the JSON by design, so meaning hidden in a comment needs a home elsewhere. Faithful conversion of bad data is still bad data: this check compares form, not truth.

Common mistakes

  • Declaring data lost when a single element is not an array. A single occurrence stays single. That is the rule working, not failing.
  • Looking for attributes under their plain names. Attributes live under @ prefixed keys, so a search for id misses what @id holds.
  • Counting the XML declaration or comments as missing content. They are not carried over. Exclude them from the XML side before comparing totals.
  • Spot-checking only the first record. The interesting failures live in the odd records: a lone repeatable element, extra attributes.
  • Comparing types instead of text. Every value arrives as text. A checker that expects numbers will report a clean conversion as broken.

FAQs

Does the converter validate my XML against a schema? No. The converter reports well-formedness errors in plain words, and that is the only automatic check. Schema validation is not part of the conversion, and the structural check in this guide is a manual method you run yourself.

Why did my number become a string in the JSON? An element that holds only text becomes that text, so a price of 89.99 arrives as the string 89.99. The conversion is structural and never guesses types. Convert the value in your own code if you need a number.

Where did an element's text go when the element also has attributes? Into a #text key inside the element's object, next to the @ prefixed attribute keys. Text that sits beside attributes or child elements always lands there, so look for #text when a value seems missing.

An element that repeats elsewhere appears once in my file and is not an array. Is that an error? No. A single occurrence stays single, while repeated elements become arrays. That is the documented behaviour, and it means shapes vary between documents. Normalise on your side if your code needs one consistent shape.

What was in the XML that will never appear in the JSON? Comments, processing instructions and namespaces are not carried over. If your check counts them as content, the totals will never match, so exclude them from the XML side before you compare.

Conclusion

A conversion earns trust when its output survives a structured comparison: counts that reconcile, arrays that match their repeats, attributes under @ keys, text in #text where the rules put it. Run that check on the first record, the last record and the strange ones, and you know what your JSON is worth. The method pairs naturally with our guide to converting an XML sitemap into JSON for analysis, where the same rules produce the file you then verify. For more guides in this series, see our Data and JSON Tools guides.

References