0 Stars
Updated Last
8 Years Ago
Started In
May 2016


Build Status



This package implements a fairly simplistic XML-to-JSON conversion. Note that at this time the XML's attributes are ignored in the parsing. Any information kept here will not be preserved in the resulting JSON.

Given the root of an XML, the xml2json() function generates an ASCIIString with the appropriate formatting for JSON. We can then write this to disk.


Consider the following simple XML document. This toy example was borrowed (with slight modification) from the LightXML package.

<?xml version="1.0" encoding="UTF-8"?>
    <title>Biography of John Adams</title>
    <author>David Smith</author>
    <author>James Jones</author>
    <title>Introduction to Templates in C++</title>
    <author>Samantha Black</author>
      <street>123 Jones Avenue</street>

Suppose we copy and paste the above into a file called ex1.xml.

Reading in XML document

using XML2JSON

filename = "ex1.xml"

# Read in XML and get its root
xdoc = parse_file(filename)
xroot = root(xdoc)


Convert to JSON

Next we simply provide the parsed XML's root to the xml2json() function.

json_string = xml2json(xroot)

This produces a the following:


        "author":["David Smith","James Jones"],
        "title":"Biography of John Adams"
        "author":"Samantha Black",
        "title":"Introduction to Templates in C++"
            "street":"123 Jones Avenue",

Write JSON to disk

Finally, we can simply print that string to disk using Julia's standard write() function.

f = open("ex1.json", "w")
write(f, json_string)

Spacing and Newline Characters

Note that the xml2json() function takes two optional arguments. The first controls the spacing of the indentation in the resulting JSON. This defaults to 4 (some prefer 8). The second optional argument (and therefore, third positional argument) controls how newline characters are handled. By default, this replaces \n with \\n in the JSON's text fields. This produces valid JSON documents.


This package is under active development. Please notify us of bugs or proposed improvements by submitting an issue or pull request.