json to xml composing and parsing convention.
Project description
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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.