Skip to main content

json to xml composing and parsing convention.

Project description

https://travis-ci.org/knowark/jsomark.svg?branch=master https://codecov.io/gh/knowark/jsomark/branch/master/graph/badge.svg

JSON <-> XML parsing and composing convention.

What is jsomark?

jsomark is a JSON to XML translation convention. jsomark accepts json in an standardized format and converts it to XML. It should as well reverse the operation and produce a JSON document from an XML one.

Usage

To create an XML from a JSON document just use the json_to_xml function:

from jsomark import json_to_xml

json_data = b'{"hello": "world"}'

xml_data = json_to_xml(json_data)

assert xml_data == b'<hello>world</hello>'

jsomark also supports more complex documents, like nested json structures and Python json serializable dictionaries:

from jsomark import json_to_xml

json_data = {
    "Company": {
        "Name": "Knowark",
        "Country": "Colombia"
    }
}

xml_data = json_to_xml(json_data)

assert xml_data == (
    b'<Company><Name>Knowark</Name><Country>Colombia</Country></Company>')

Attributes

XML can carry more information (i.e. metadata) than JSON, that is why a convention in the format of a converting json document is needed to match the original XML semantics. In jsomark, attributes are defined with the symbol “&”:

Note

Attribute values can only be text or bytes

from jsomark import json_to_xml

json_data = {
    "Device": {
        "Reference": {
            "&": {"ID": "XYZ2020", "Serial": "S10987"}
        }
    }
}

xml_data = json_to_xml(json_data)

assert xml_data == (
    b'<Device><Reference ID="XYZ2020" Serial="S10987"/></Device>')

If the key with attributes also has a text content, then the symbol “#” should be used to carry it:

from jsomark import json_to_xml

json_data = {
    "Employee": {
        "Company": {
            "&": {"VAT": "900123765"},
            "#": "Servagro"
        }
    }
}

xml_data = json_to_xml(json_data)

assert xml_data == (
    b'<Employee><Company VAT="900123765">Servagro</Company></Employee>')

Note

If a JSON key doesn’t have attributes, its value becomes the text of the resulting XML element as seen in the previous examples.

Lists

Lists in the JSON document are interpreted as repeating elements inside the generated XML.

from jsomark import json_to_xml

json_data = {
    "Order": {
        "Line": [
            {"&": {"ID": "1"}, "#": "Chocolate Ice Cream"},
            {"&": {"ID": "2"}, "#": "Banana Split"},
            {"&": {"ID": "3"}, "#": "Caramel Cake"}
        ]
    }
}

xml_data = json_to_xml(json_data)

assert xml_data == (
    b'<Order><Line ID="1">Chocolate Ice Cream</Line>'
    b'<Line ID="2">Banana Split</Line>'
    b'<Line ID="3">Caramel Cake</Line></Order>')

Namespaces

In jsomark, namespaces are provided as a separate dictionary whose keys are the prefixes that must be used in the json document itself. The default namespace should be set in the ‘None’ key of the namespaces dictionary and its keys in the json document don’t have to be prefixed:

Note

Don’t miss the “:” separator in the non-default namespaced key such as ‘isbn:number’ in the following example.

from jsomark import json_to_xml

namespaces = {
    None: 'urn:loc.gov:books',
    'isbn': 'urn:ISBN:0-395-36341-6'
}

json_data = {
    "book": {
        "title": "Cheaper by the Dozen",
        "isbn:number": 1568491379
    }
}

xml_data = json_to_xml(json_data, namespaces=namespaces)

assert xml_data == (
    b'<book xmlns="urn:loc.gov:books" xmlns:isbn="urn:ISBN:0-395-36341-6">'
    b'<title>Cheaper by the Dozen</title>'
    b'<isbn:number>1568491379</isbn:number></book>'

Project details


Download files

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

Files for jsomark, version 0.2.2
Filename, size File type Python version Upload date Hashes
Filename, size jsomark-0.2.2-py3-none-any.whl (5.2 kB) File type Wheel Python version py3 Upload date Hashes View
Filename, size jsomark-0.2.2.tar.gz (4.6 kB) File type Source Python version None Upload date Hashes View

Supported by

Pingdom Pingdom Monitoring Google Google Object Storage and Download Analytics Sentry Sentry Error logging AWS AWS Cloud computing DataDog DataDog Monitoring Fastly Fastly CDN DigiCert DigiCert EV certificate StatusPage StatusPage Status page