Registry / serialization / xmljson

xmljson

JSON →
library0.2.1pypypi✓ verified 24d ago

xmljson is a Python library that converts XML into JSON/Python dictionaries/arrays and vice-versa. It offers various conversion conventions like BadgerFish, Abdera, Cobra, GData, Parker, and Yahoo to manage the mapping of XML structures to JSON. The library's current version is 0.2.1, but it is explicitly marked as not actively maintained and its GitHub repository is archived, indicating an abandoned status.

pip install xmljson
INSTALL
IMPORT
SIG · XMLJSON
X
xmljson
serializationpythonv0.2.1
Install
2.2s avg
Import
49ms
Disk
16MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v0.2.1 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.103.95 runs
installs and imports cleanly · install 0.0s · import 0.054s · 17.8MB
glibc
py 3.103.95 runs
installs and imports cleanly · install 2.2s · import 0.044s · 18MB
16MB installed
● package 16MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

badgerfish
from xmljson import badgerfish as bf
BadgerFish is one of several conversion conventions provided.
gdata
from xmljson import gdata
parker
from xmljson import parker

This quickstart demonstrates converting an XML string to a Python dictionary (which can then be serialized to JSON) and converting the dictionary back to an XML ElementTree object, using the BadgerFish convention. It highlights how attributes (e.g., 'value') are prefixed with '@' and text content with '$' in the resulting dictionary structure.

from xml.etree.ElementTree import fromstring, tostring from xmljson import badgerfish as bf import json # Example XML string xml_string = '<employees><person><name value="Alice"/></person><person><name value="Bob"/></person></employees>' # Convert XML to a Python dictionary using BadgerFish convention xml_element = fromstring(xml_string) data_dict = bf.data(xml_element) print('Python dict (BadgerFish):') print(json.dumps(data_dict, indent=2)) # Convert Python dictionary back to XML xml_from_dict = bf.etree(data_dict) print('\nXML from dict (BadgerFish):') print(tostring(xml_from_dict[0]).decode())
Debug
Known issues
breakingThe xmljson library is explicitly marked as not actively maintained, and its GitHub repository is archived, making it effectively abandoned. There will be no further updates, bug fixes, or compatibility assurances for this library. Users should consider migrating to actively maintained alternatives.
fix
Migrate to alternative libraries such as `xmltodict` or `untangle` for XML-to-JSON conversion.
affects: 0.2.1 and older
gotchaxmljson supports multiple XML-to-JSON conversion conventions (e.g., BadgerFish, GData, Parker). The choice of convention significantly alters the resulting JSON structure, particularly for attributes, text content, and nested elements. For example, BadgerFish prefixes attributes with `@` and text with `$`.
fix
Carefully review the documentation for each convention and select the one that best suits your desired JSON output format. Be prepared to transform the output further if no convention perfectly matches your needs.
affects: All versions
gotchaConverting XML to JSON can lead to inherent challenges due to structural differences (e.g., XML's attributes vs. JSON's key-value pairs, ordered XML elements vs. unordered JSON object properties, mixed content). This can result in data loss or non-intuitive JSON representations if not handled carefully. Mixed content is often represented using special keys like `$` or `#text`.
fix
Understand the limitations of automatic conversion. Validate the output JSON against your expectations. If complex XML structures like mixed content, namespaces, or varying cardinalities are present, manual post-processing of the generated JSON or using a library that offers more fine-grained control may be necessary.
affects: All versions
gotchaXML namespaces are not explicitly handled as a first-class feature across all conventions, which can be a significant issue when dealing with XML documents that heavily rely on namespaces. This might lead to unexpected keys or loss of namespace information in the JSON output.
fix
For XML with complex namespace requirements, it is crucial to test `xmljson`'s behavior with your specific documents and chosen convention. If it falls short, consider preprocessing the XML to remove or simplify namespaces, or use an alternative library with explicit namespace handling capabilities.
affects: All versions
Errors
Common errors & fixes
ModuleNotFoundError: No module named 'xmljson'
The 'xmljson' library is not installed in the current Python environment.
fix
pip install xmljson
AttributeError: 'str' object has no attribute 'tag'
The conversion functions like `badgerfish.data()` or `gdata.data()` expect an `xml.etree.ElementTree.Element` object, but a raw XML string was passed directly.
fix
First parse the XML string into an ElementTree object using `xml.etree.ElementTree.fromstring()` before passing it to the conversion function.

```python
import xml.etree.ElementTree as ET
from xmljson import badgerfish

xml_string = '<root><item>test</item></root>'
xml_element = ET.fromstring(xml_string)
json_data = badgerfish.data(xml_element)
```
AttributeError: 'str' object has no attribute 'items'
The conversion functions like `badgerfish.etree()` or `gdata.etree()` expect a Python dictionary or list of dictionaries as input (representing JSON data), but a raw JSON string was passed directly.
fix
First parse the JSON string into a Python dictionary or list using `json.loads()` before passing it to the conversion function.

```python
import json
from xmljson import badgerfish

json_string = '{"root": {"item": "test"}}'
json_data = json.loads(json_string)
xml_element = badgerfish.etree(json_data)
```
AttributeError: module 'xmljson' has no attribute 'badgerfish'
The conversion conventions (like 'badgerfish', 'parker') are functions imported directly from the 'xmljson' package, not attributes of the 'xmljson' module object itself.
fix
from xmljson import badgerfish
# Then use: badgerfish.data(...)
xml.etree.ElementTree.ParseError: not well-formed (invalid token): line 1, column 0
The input string provided to 'xmljson's 'data()' method is not valid XML or is malformed.
fix
from xmljson import badgerfish
xml_string = "<root><item>value</item></root>" # Ensure valid XML
json_output = badgerfish.data(xml_string)
Upgrade
Version history
0.2.1latest on PyPI · released Apr 25, 2020
Audit
Dependencies

No dependency data recorded yet.

Agent activity
25 hits · last 30 days
node
22
OpenAI (training)
1
Resources
xmljson — pip install xmljson · libregistry