Skip to main content

A Library to convert JSON into XML format.

Project description

JsonToXML

A module to convert JSON to XML format.

Details

The module contains functions to convert a JSON File/String to an XML File/String. In case the XML is to be processed further, the pointer root node containing the XML data can also be returned using the specific function.

The module has following functions:

Function Description
fromFile( filename [, rootName ]) Takes JSON data from the specified filename and returns the root pointer.
fromText( text [, rootName ]) Takes JSON data in form of a string and returns the root pointer.
fromFiletoFile( inputFile, outputFile [, rootName ]) Takes JSON data from the specified inputFile and writes the XML data to the specified outputFile.
fromTexttoFile( text, outputFile [, rootName ]) Takes JSON data in form of a string and writes the XML data to the specified outputFile.
fromFiletoText( inputFile [, rootName ]) Takes JSON data from the specified inputFile and returns the XML data in form of a string.
fromTexttoText( text [, rootName ]) Takes JSON data in form of a string and returns the XML data as a string.

Note: The root node of the XML data is by default "root", but can be specified by passing the argument rootName = "Custom Name" to the function.

This Module works well with Python 3

Installation

The module can be installed using the pip command:

pip install JsonToXML

Usage

The usage of Module is pretty simple, you have to just import the library and use the functions directly.

fromFile( file [, rootName ] )

import JsonToXML
import xml.etree.cElementTree as ET
import xml.dom.minidom
root = JsonToXML.fromFile("example.json", rootName="Employees") # convert the file to XML and return the root node
xmlData = ET.tostring(root, encoding='utf8',method='xml').decode() # convert the XML data to string
dom = xml.dom.minidom.parseString(xmlData) 
prettyXmlData = dom.toprettyxml() # properly format the string of XML data
print(prettyXmlData) # print the formatted XML data

The above program reads JSON data from a file and pretty prints (With proper Indentation and line breaks) XML data on the screen.

fromText( text [, rootName ] )

import JsonToXML
import xml.etree.cElementTree as ET
import xml.dom.minidom
exampleJSON = "{ 'employee': { 'name':'sonoo', 'salary':56000, 'married':true } }"
root = JsonToXML.fromText(exampleJSON) # convert the string to XML and return the root node
xmlData = ET.tostring(root, encoding='utf8',method='xml').decode() # convert the XML data to string
dom = xml.dom.minidom.parseString(xmlData) 
prettyXmlData = dom.toprettyxml() # properly format the string of XML data
print(prettyXmlData) # print the formatted XML data

The above program reads JSON from a string and prints the XML data on the screen.

fromFiletoFile( inputFile, outputFile [, rootName ] )

import JsonToXML
JsonToXML.fromFiletoFile("example1.json","example2.xml") # converts the JSON data from the file to XML and writes it on the XML file.

The above program simply converts the JSON data from the inputFile and writes XML data to the outputFile.

fromTexttoFile( text, outputFile [, rootname ] )

import JsonToXML
exampleJSON = "{ 'employee': { 'name':'sonoo', 'salary':56000, 'married':true } }"
JsonToXML.fromFiletoFile(exampleJSON,"example2.xml") # converts the JSON data from the string to XML and writes it on the XML file.

The above program simply converts the JSON data from the string and writes XML data to the outputFile.

fromFiletoText( inputFile [, rootName ] )

import JsonToXML
xmlData = JsonToXML.fromFiletoText("example1.json") # converts the JSON data from the file to XML and returns string of XML data.
print(xmlData)

The above program reads JSON data from a file and returns a formatted string of XML data.

fromTexttoText( inputFile [, rootName ] )

import JsonToXML
exampleJSON = "{ 'employee': { 'name':'sonoo', 'salary':56000, 'married':true } }"
xmlData = JsonToXML.fromTexttoText(exampleJSON) # converts the JSON data from the string to XML and returns string of XML data.
print(xmlData)

The above program reads JSON data from a string and returns a formatted string of XML data.

Author

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

JsonToXML-0.0.2.tar.gz (2.8 kB view details)

Uploaded Source

Built Distribution

JsonToXML-0.0.2-py3-none-any.whl (3.8 kB view details)

Uploaded Python 3

File details

Details for the file JsonToXML-0.0.2.tar.gz.

File metadata

  • Download URL: JsonToXML-0.0.2.tar.gz
  • Upload date:
  • Size: 2.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.32.2 CPython/3.6.0

File hashes

Hashes for JsonToXML-0.0.2.tar.gz
Algorithm Hash digest
SHA256 b0b44bb5341c1fbe03fbdb37c60571e736d452fcab8fcb2f8b667a51ca3744dc
MD5 1ee6d44512843c5c3ffd533265a88d7b
BLAKE2b-256 689258b48803c151ad95320bdda9ca22a2ce63e93a553277f30fd8476cd56435

See more details on using hashes here.

File details

Details for the file JsonToXML-0.0.2-py3-none-any.whl.

File metadata

  • Download URL: JsonToXML-0.0.2-py3-none-any.whl
  • Upload date:
  • Size: 3.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.32.2 CPython/3.6.0

File hashes

Hashes for JsonToXML-0.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 4ed0e5aedb81aeabc96f5c6d2b00c0fdaa54dcd15e5f37169b62f93f5748331f
MD5 6d0aa52c4482d93dac0c5f6798c262c3
BLAKE2b-256 bcc4279cc34fb9c1d9b05c913328b7c8ddb87e62fb697780e96c388c060cbf66

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page