DEV Community

R. Tyler Croy
R. Tyler Croy

Posted on • Originally published at brokenco.de

2 2

Converting XML to JSON in Rust

I generally default to using JSON for data interchange but there are still a myriad of formats of XML out there, for which I have created the xmltojson crate. I originally wrote this one night to help me get an XML dataset into JSON so that I could use PostgreSQL's JSONB column type, but I only recently published it to crates.io since it may be useful for others.

Cargo.toml

[dependencies]
xmltojson = "0"
Enter fullscreen mode Exit fullscreen mode

The xmltojson crate implements Stefan Goessner’s xml2json which results in a fairly straightforward conversion of XML data structure into JSON. Take the following XML for example:

<ol class="xoxo">
    <li>Subject 1
        <ol>
            <li>subpoint a</li>
            <li>subpoint b</li>
        </ol>
    </li>
    <li>
        <span>Subject 2</span>
        <ol compact="compact">
            <li>subpoint c</li>
            <li>subpoint d</li>
        </ol>
    </li>
</ol>
Enter fullscreen mode Exit fullscreen mode

This XML structured will be rapidly converted into the following JSON, with
attributes and children encoded into the structure:

{
    "ol": {
        "@class":"xoxo",
        "li": [
            {
                "#text":"Subject 1",
                "ol":{
                    "li":[
                        "subpoint a",
                        "subpoint b"
                    ]
                }
            },
            {
                "span":"Subject 2",
                "ol": {
                    "@compact":"compact",
                    "li": [
                        "subpoint c",
                        "subpoint d"
                    ]
                }
            }
        ]
    }
}
Enter fullscreen mode Exit fullscreen mode

There are some oddities with the JSON encoding of XML, particularly around CDATA, but overall I have been quite pleased turning XML into JSON which I can more easily query with jq, PostgreSQL, or even ingest into Elasticsearch.

If you happen to find any bugs, please submit pull requests on GitHub

Hostinger image

Get n8n VPS hosting 3x cheaper than a cloud solution

Get fast, easy, secure n8n VPS hosting from $4.99/mo at Hostinger. Automate any workflow using a pre-installed n8n application and no-code customization.

Start now

Top comments (0)

Billboard image

Create up to 10 Postgres Databases on Neon's free plan.

If you're starting a new project, Neon has got your databases covered. No credit cards. No trials. No getting in your way.

Try Neon for Free →

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay