From e2854788cf78cc61f52ad76688f897469ccb2432 Mon Sep 17 00:00:00 2001 From: R Tyler Croy Date: Sun, 23 May 2021 10:19:02 -0700 Subject: [PATCH] xmltojson --- _posts/2021-05-23-converting-xml-to-json.md | 81 +++++++++++++++++++++ 1 file changed, 81 insertions(+) create mode 100644 _posts/2021-05-23-converting-xml-to-json.md diff --git a/_posts/2021-05-23-converting-xml-to-json.md b/_posts/2021-05-23-converting-xml-to-json.md new file mode 100644 index 0000000..62e26c6 --- /dev/null +++ b/_posts/2021-05-23-converting-xml-to-json.md @@ -0,0 +1,81 @@ +--- +layout: post +title: "Converting XML to JSON in Rust" +tags: +- 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](https://crates.io/crates/xmltojson) crate. I originally wrote this +one night to help me get an XML dataset into JSON so that I could use +[PostgreSQL's](https://postgresql.org) `JSONB` column type, but I only recently +published it to [crates.io](https://crates.io) since it may be useful for +others. + +**Cargo.toml** +```toml +[dependencies] +xmltojson = "0" +``` + + +The `xmltojson` crate implements [Stefan Goessner’s xml2json](https://goessner.net/download/prj/jsonxml/) which results in a fairly straightforward conversion of XML data structure into JSON. Take the following XML for example: + + +```xml +
    +
  1. Subject 1 +
      +
    1. subpoint a
    2. +
    3. subpoint b
    4. +
    +
  2. +
  3. + Subject 2 +
      +
    1. subpoint c
    2. +
    3. subpoint d
    4. +
    +
  4. +
+``` + +This XML structured will be rapidly converted into the following JSON, with +attributes and children encoded into the structure: + +```json +{ + "ol": { + "@class":"xoxo", + "li": [ + { + "#text":"Subject 1", + "ol":{ + "li":[ + "subpoint a", + "subpoint b" + ] + } + }, + { + "span":"Subject 2", + "ol": { + "@compact":"compact", + "li": [ + "subpoint c", + "subpoint d" + ] + } + } + ] + } +} +``` + +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](https://github.com/rtyler/xmltojson)