simple-xml-to-json

A tiny, dependency-free library that transpiles XML into JSON through an AST.

Library
npm
v1.2.7
30stars
MIT License

Repository Health

Pre-computed score based on development activity, maintenance, community, maturity, and trend momentum.How we score it →
24/100Needs Attention
Development Activity8
Maintenance0
Community16
Maturity60
Momentum12

Technical Analysis

AI-assessed by reading the actual repository — architecture, code quality, innovation, and documentation.How we score it →
79/100Good
Architecture80
Code Quality78
Innovation68
Learning Curve90

simple-xml-to-json is a lightweight JavaScript library for converting XML strings into JSON. It first tokenizes and parses the XML into an abstract syntax tree, then runs a converter over that tree to produce plain JSON, mapping element attributes to properties and text content to a dedicated field.

Because the parse and convert stages are decoupled, you can supply your own converter to transform the AST into any output format — YAML, a custom object shape, or anything else — instead of the default JSON. The package ships both CommonJS and ESM builds, has no runtime dependencies, and is TypeScript-compatible.

What You Get

  • A convertXML() function that transpiles an XML string to JSON out of the box
  • A createAST() function exposing the parsed abstract syntax tree for custom processing
  • Pluggable converters so you can emit YAML or any custom shape from the same AST
  • Dual CommonJS and ESM builds with bundled TypeScript type definitions
  • A zero-dependency footprint suitable for both Node.js and the browser

Common Use Cases

  • Converting XML API responses or config files into JSON for JavaScript apps
  • Building custom XML-to-anything transformers on top of the exposed AST
  • Parsing XML in the browser without pulling in a heavy dependency tree
  • Normalizing legacy XML data into JSON during migrations or ETL scripts

Under The Hood

Architecture - The library is a small two-stage pipeline. src/lexer.js tokenizes the raw XML string, src/transpiler.js consumes those tokens to build an abstract syntax tree (the model defined in src/model.js), and a converter in src/converters/astToJson.js walks the tree to emit JSON. convertXML() chains these stages, while createAST() stops at the tree so callers can plug in their own converter.

Tech Stack - Plain JavaScript with no runtime dependencies, bundled with Rollup into CommonJS and ESM outputs plus hand-written TypeScript typings under typings/. ESLint enforces style and the project targets both Node.js and browser environments.

Code Quality - The codebase is compact (~550 lines of source) and cleanly separated by responsibility (lexer, transpiler, model, converter). It ships a Jest test suite covering the lexer, transpiler, and ESM entry point, with coverage reported through Codecov.

API Design - The surface is just two well-named functions and an optional converter callback, so basic conversion is a single call with no configuration. The AST-plus-converter design keeps the common case trivial while leaving an escape hatch for advanced custom output formats.

Join founders buildingwith open source

Opinionated takes, migration guides, cost-saving tips, and insights from the open source ecosystem.

Subscribe on Substack
Join 750+ subscribers

Search