npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

@mattlag/xmltojson

v2.0.2

Published

Convert XML to JSON in JavaScript.

Readme

XMLtoJSON

I know there are probably 13.5 Million of these already, but I wanted to do one myself, okay?

How to use

1. Use locally (direct .js import)

If you have the XMLtoJSON.js file locally, you can import and use it as an ES module:

// Import the function (ES module)
import { XMLtoJSON } from './XMLtoJSON.js';

const xmlString = `<note><to>User</to><from>AI</from><body>Hello!</body></note>`;
const result = XMLtoJSON(xmlString);
console.log(result);

2. Install from npm (scoped package)

First, install the package:

npm install @mattlag/xmltojson

Then import and use it in your project:

// Import from node_modules (ES module)
import { XMLtoJSON } from '@mattlag/xmltojson';

const xmlString = `<note><to>User</to><from>AI</from><body>Hello!</body></note>`;
const result = XMLtoJSON(xmlString);
console.log(result);

Note: The package is published under the @mattlag scope. Use @mattlag/xmltojson in your import and install commands.

Optional trim argument

The XMLtoJSON function accepts a second argument, trimOptions, which lets you control how whitespace, newlines, and tabs are handled in the parsed output. This is useful for cleaning up text content from XML nodes.

Available options:

  • trimWhitespace (default: true): Remove leading and trailing spaces from text.
  • trimNewlines (default: true): Remove all newline characters (\n, \r).
  • trimTabs (default: true): Remove all tab characters (\t).

If you omit trimOptions, all three are enabled by default.

Resulting xml_tag data structure

{
    name : 'xml_tag_name',
    attributes : {
        'xml_attribute_name1' : 'XML Attribute value1',
        'xml_attribute_name2' : 'XML Attribute value2',
        'xml_attribute_name3' : 'XML Attribute value3',
        ...
    },
    content : [
        { xml_tags },
        { xml_tags },
        ...,
        'text content'
    ]
}

A proper JavaScript SyntaxError will be thrown if the XML is not properly formatted.

Things to consider

XMLtoJSON is really focused on data, and not so much the metadata. So I did this:

  • All comments are ignored <!-- stuff like this -->
  • All declarations are ignored, stuff like <?xml version="1.0" encoding="UTF-8"?>
  • All CDATA sections are ignored, stuff like <![CDATA[<greeting>Hello, world!</greeting>]]>
  • Newline and Tab characters, like \r \n \t, are blindly removed from everywhere

License & Copyright

Copyright © 2026 Matthew LaGrandeur

Released under GPL 3.0

Author

| Matthew LaGrandeur's picture | | ---------------------------------------------------------------------------------------------------- | | Matthew LaGrandeur | | matt[at]mattlag[dot]com |