Skip to content

[Personal Project] Django Web App that converts an XML to JSON through Front-End and API.

Notifications You must be signed in to change notification settings

Alfareiza/xml-to-json

Repository files navigation

XML o Json Converter
GitHub stars GitHub followers

Setting Up

Install Python 3.7 or later if it is not already installed. Then, set up and enter a virtual environment and run pip install -r requirements.txt to install the dependencies. You should then be able to run the project from the exercise directory by running python manage.py runserver.

To verify that the server is running correctly, visit http:127.0.0.1:8000 in your browser.

Guidelines

This Django project consists of two parts:

  1. A very simple HTML page at /connected/.
  2. A stubbed out API endpoint at /api/converter/convert/

My tasks were:

  1. Add a file input to the HTML page and modify the view so that, when a file is submitted, convert it to JSON and return that to the user
  2. Add the same functionality to the API endpoint, returning the converted JSON as an application/json response.

To test my solution, I run python manage.py test. This will execute 3 tests, which attempt to submit a file and check the response.

As a good solution will not only pass the tests, but also work on any user-submitted XML file. Furthermore, a good solution should never return a 500 error and should show an error message to the user if they upload an invalid XML file.

JSON Conversion

For the purposes of this exercise, was ignored any XML attributes. I'm only interested in converting the XML node tags and any text values into JSON.

Leaf nodes were converted into a JSON object with the node tag as the key and the node's text value as the value. For example, <Foo>Bar</Foo> should be converted to {"Foo": "Bar"}.

Non-leaf nodes were converted into a JSON object with the node name as the key and an array of the node's children as the value. For example:

<Foo>
    <Bar>Baz</Bar>
</Foo>

should be converted to

{
    "Foo": [
        {"Bar": "Baz"}
    ]
}

The tests provide additional examples of more complex conversions.

Images

Uploading the xml

image

XML Converted

image

About

[Personal Project] Django Web App that converts an XML to JSON through Front-End and API.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published