0% found this document useful (0 votes)
12 views10 pages

Convert JSON to XML in Python

Uploaded by

ccode1942
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views10 pages

Convert JSON to XML in Python

Uploaded by

ccode1942
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Securin Inc.

Python XML JSON Test

1
1 Introduction

This assignment entails creating a simple Python program that will allow users to give arbitrary
JSON data and have it converted to XML according to the following standard.
In order to complete this assignment you must perform the following tasks.
● Provide a build script of some type to build your application.
● Provide a simple command line interface for running your program. Each of the
aforementioned items is elaborated in the following sections. In general, the more you treat
this project as though it were going to be real production code the better. You program MUST
output valid XML!

2 JSON to XML Specification

2.1 Overview

JSON does not strictly map to XML in a single way. There are many different ways one could create a
mapping from a JSON document to a XML document. This section describes the mapping that your
code MUST follow in order to be considered valid.

2.2 General Constraints

In JSON objects you are given name value pairs. This can be mapped to XML in several ways. For
this program, the XML element name corresponds to the type of the element, not the name of the
element. The name of the JSON element in a JSON object is given as an attribute on the XML
element. Only JSON array and object values may be at the top level of a file. The specifics of these
values are discussed in greater detail in later sections.

2.3 JSON Types

2
JSON supports the following types, all of which must be mapped into your XML output. Only JSON
objects and arrays are supported as top level values.

2.3.1 Number

A JSON element with a number as a value should map to an XML element named <number>, with the
number as the single and only value between the opening and closing tags. An example of this is
shown in figure 2 and 3.

2.3.2 String

A JSON element with a string as a value should map to an XML element named <string>, with the
string as the single and only value between the opening and closing tags. An example of this is shown
in figure 4 and 5.

2.3.3 Boolean

3
A JSON element with a Boolean as a value should map to an XML element named <boolean>, with
the boolean as the single and only value between the opening and closing tags. The only valid
boolean values are true and [Link] example of this is shown in figure 6 and 7.

4
5
2.3.4 Array

A JSON element with an array as a value should map to an XML element named <array>. For each
element in the JSON array there should be a corresponding XML sub-element denoting the value and
type. A JSON array may contain arbitrary sub values. type. Elements of the array should not have a
name attribute. Remember, JSON arrays are not required to be homogeneous! An example of this is
shown in figure 8 and 9.

2.3.5 Object

A JSON element with an object as a value should map to an XML element named <object>. Objects
can contain arbitrary [Link] example of this is shown in figure 10. Objects are the only JSON
types that generate name attributes on XML tags.

6
7
8
2.3.6 null

A JSON element with an null as a value should map to an XML element named <null/>. Unlike other
XML representations for JSON values, a null value does not have a closing tag, rather it is self-closing
by adding a forward slash before the end of the element tag. An example of this is shown in figure 11
and 12.

2.3.7 Examples

Besides the examples shown here, there are example files in the examples/ folder in the archive with
which you were provided. These were generated with our own reference implementation. If anything
in those files seems to not match up with this document, please contact us and let us know.

3 Python Code

3.1 Open Source Libraries

You may use any open source libraries to help you accomplish this project without any restrictions,
however You must either include these libraries in your submitted code, or have them automatically
downloaded through your build tool.

3.2 CLI

Your program must have a very simple command line interface. The first argument is taken to be the
path to the input JSON file and the second argument is taken to be the path to the XML output file.

9
Any other number of arguments should print an usage message. The XML file may or may not exist,
but the path must be valid.

3.3 Test and Auxiliary Files

If you use or create any test files or any other auxiliary files as part of your submission, we would very
much like to inspect those as well. Please include them in the submission.

4.4 README

Make sure to include a README file in your project that explains how to build and run your project.
Please name this file README, please make sure that it is in plain text format, and please place it in
the root (top-level) of your project turn in. This file must explain exactly how to build and run your
[Link] must also list all libraries that you used, including a URL so that we can look them up. You
may include a short writeup in this file describing your general design motivations. Please mention the
location and purpose of any test or auxiliary files you have included in your submission.

5 Submitting Your Project

Please submit your project as a .zip, .[Link], .[Link], or .[Link]. Please do not include any compiled
code, unless it is an library.

6 Questions
If you have any questions or need clarification about anything please feel free to send an email

10

Common questions

Powered by AI

JSON objects map to XML using an <object> tag, with sub-elements representing JSON name-value pairs as attributes on the corresponding XML elements. This inclusion of name attributes is unique to objects in the conversion process and allows for structured representation in XML .

The README file must be in plain text format and located at the root of the project. It must explain how to build and run the project, list used libraries with URLs for reference, and optionally include a writeup on design motivations. The README should also mention the location and purpose of any test or auxiliary files included .

The inclusion of test and auxiliary files is encouraged as part of the project submission. These should assist in demonstrating the correctness and functionality of the code. Their location and purpose must be documented in the README file, ensuring clarity for reviewers .

The mapping from JSON to XML should use the following XML element names: For a JSON number, use <number>; for a string, use <string>; for a boolean, use <boolean>; for an array, use <array> with sub-elements indicating values and types; for an object, use <object> with name attributes on XML tags; and for a null value, use a self-closing <null/> element .

The use of open-source libraries is allowed without restrictions, provided they are included in the submitted code or automatically downloaded via a build tool. This means that developers can employ libraries to facilitate implementation as long as these requirements are met .

JSON null values should convert to a self-closing XML element named <null/>. Unlike other JSON types, null does not have an opening and closing tag pair. This approach reflects the nature of null as a lack of value, emphasizing brevity and precision in XML output .

The command line interface for the JSON to XML conversion program should accept two arguments: the first is the path to the input JSON file, and the second is the path to the intended XML output file. If arguments are provided in excess, the program should display a usage message. It is implied that the XML file may not exist initially, but the path must be valid .

If any discrepancies arise between example files and the specification document, developers are advised to reach out and report these issues via email for clarification. This ensures that the implementation aligns correctly with the intended specifications and avoids confusion .

The project should be submitted as a compressed archive, such as .zip, .tar.gz, .tar.bz, or .tar.xz. Compiled code should not be included unless it is a library. This is to ensure that the submission contains only relevant and necessary source files .

JSON arrays convert to XML with an <array> element containing sub-elements for each array item. These sub-elements are based on their respective types and do not have a name attribute. The conversion accommodates heterogeneity due to JSON's nature of not requiring arrays to be homogeneous .

You might also like