XML repeated elements: when to use arrays in JSON output
XML expresses lists by repeating a tag. JSON expresses lists with arrays. The tricky part is consistency: one item vs multiple items can produce different shapes.
Why “sometimes array, sometimes object” is painful
If your JSON output uses an object when there’s one element and an array when there are multiple, every consumer has to write branching logic. This creates bugs in spreadsheets, scripts, and API pipelines.
Practical mapping rule
- If a tag appears more than once under the same parent → represent it as an array.
- If it appears once → represent it as a single object or string.
- If you control the output contract → prefer always-array for known “list” tags.
How to keep output stable
For stable downstream code, document which tags are lists. If you can’t, normalize after conversion: wrap single objects into arrays for expected list fields. This is especially useful for feeds where “one item” is common during tests.
Practical checklist (fast)
If you’re stuck, use this quick checklist to narrow the problem before you try “random fixes”. Start by validating the input format (syntax first), then confirm shape expectations (array vs object, headers vs rows). Convert a small sample, inspect the output, and only then export the full result.
- Validate: confirm the input is strict JSON/XML/CSV (no stray characters).
- Confirm shape: arrays vs objects; headers vs row lengths; repeated tags vs arrays.
- Test a sample: first 20–50 rows/items are enough to detect parsing issues.
- Export: copy/download the output and re-check it in the consumer (script/spreadsheet/API).
This workflow is privacy-first by design: All processing happens locally in your browser. Files are never uploaded.
FAQ
What about mixed content (text + children)? Store text under a dedicated key (like #text) and keep children separate.
Do namespaces affect array detection? They can: prefixes make tag names look different. Ensure consistent naming before grouping.
Trust note: All processing happens locally in your browser. Files are never uploaded.
Local verification snippet
Run a quick local check before export/convert:
const doc = new DOMParser().parseFromString(xmlText, 'application/xml');
const err = doc.querySelector('parsererror');
if (err) throw new Error(err.textContent || 'Invalid XML');
console.log('XML is well-formed');
Related by intent
Closest pages and hubs to accelerate crawl discovery and first impressions.
Quick fix checklist
- Reproduce the error on a minimal input.
- Check type/format and field mapping.
- Apply the smallest safe fix.
- Validate on production-like payload.
Related by intent
Useful follow-up pages selected from real search impressions and no-click opportunities.
Related by winning cluster
Linked from a winner family to push crawl and first-impression conversion.
Next pages to check
Closest crawled pages without impressions yet. Added to speed first-impression conversion.