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 “&”:

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>')

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:

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.

Source Distribution

jsomark-0.2.2.tar.gz (4.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

jsomark-0.2.2-py3-none-any.whl (5.2 kB view details)

Uploaded Python 3

File details

Details for the file jsomark-0.2.2.tar.gz.

File metadata

  • Download URL: jsomark-0.2.2.tar.gz
  • Upload date:
  • Size: 4.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.0.5 CPython/3.6.9 Linux/4.15.0-88-generic

File hashes

Hashes for jsomark-0.2.2.tar.gz
Algorithm Hash digest
SHA256 2ba5caf8519edc9d5dd06254c4c738905cb6a819a63e7b15f88e9f91d67d8ca2
MD5 1787251dbe2c7ff0142fb2a51a840831
BLAKE2b-256 9178cfbe95ab4d2b1dcd6ba684c74aec0cbf37b0ffb3335dda8db60b44815e71

See more details on using hashes here.

File details

Details for the file jsomark-0.2.2-py3-none-any.whl.

File metadata

  • Download URL: jsomark-0.2.2-py3-none-any.whl
  • Upload date:
  • Size: 5.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.0.5 CPython/3.6.9 Linux/4.15.0-88-generic

File hashes

Hashes for jsomark-0.2.2-py3-none-any.whl
Algorithm Hash digest
SHA256 6def62a7229f4257241566729746232f78df15e60d5a31cbb16dfea8fb5e9a6a
MD5 a546712d66cb6c2a0732bc56518ba30b
BLAKE2b-256 db4cfa238008ce6282b1410c6f50e403126038dde60717bd784590a9478dd6a4

See more details on using hashes here.

Supported by

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