Skip to main content

jsomark

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

Metadata

Release files for jsomark 0.2.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for jsomark 0.2.2
File Size Uploaded
jsomark-0.2.2.tar.gz 4.6 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for jsomark 0.2.2
File Interpreter ABI Platform
jsomark-0.2.2-py3-none-any.whl Python 3 none any Details

Total release size: 9.8 kB

Release files / jsomark-0.2.2.tar.gz

Download URL jsomark-0.2.2.tar.gz
Size 4.6 kB
Tags Source
SHA-256 checksum
How to use checksums
2ba5caf8519edc9d5dd06254c4c738905cb6a819a63e7b15f88e9f91d67d8ca2
BLAKE2b-256 checksum
How to use checksums
9178cfbe95ab4d2b1dcd6ba684c74aec0cbf37b0ffb3335dda8db60b44815e71
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.0.5 CPython/3.6.9 Linux/4.15.0-88-generic

Release files / jsomark-0.2.2-py3-none-any.whl

Download URL jsomark-0.2.2-py3-none-any.whl
Size 5.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
6def62a7229f4257241566729746232f78df15e60d5a31cbb16dfea8fb5e9a6a
BLAKE2b-256 checksum
How to use checksums
db4cfa238008ce6282b1410c6f50e403126038dde60717bd784590a9478dd6a4
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.0.5 CPython/3.6.9 Linux/4.15.0-88-generic

Release history Release notifications | RSS feed

This release

0.2.2 This release

2 release files

0.2.1

2 release files

0.2.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page