Registry / serialization / xmltojson

xmltojson

JSON →
library2.0.3pypypi✓ verified 86d ago

xmltojson is a Python module and command-line tool designed to quickly convert XML text or files into JSON format. It aims to provide a straightforward conversion mechanism for common use cases. The current stable version is 2.0.3, with releases typically focusing on stability and minor feature enhancements.

pip install xmltojson
INSTALL
IMPORT
SIG · XMLTOJSON
X
xmltojson
serializationpythonv2.0.3
Install
1.6s avg
Import
146ms
Disk
16MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v2.0.3 · 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.920 runs
installs and imports cleanly · install 0.0s · import 0.154s · 17.9MB
glibc
py 3.103.920 runs
installs and imports cleanly · install 1.6s · import 0.138s · 18MB
16MB installed
● package 16MB
Code
Verified usage

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

xmltojson
import xmltojson

Demonstrates how to convert a simple XML string to JSON using the `parse` method.

import xmltojson xml_string = '<person><name>Alice</name><age>30</age></person>' json_output = xmltojson.parse(xml_string) print(json_output) # Expected output: {"person": {"name": "Alice", "age": "30"}} (exact formatting may vary)
xmltojson --version
Debug
Known issues
gotchaConversion of single-element XML lists to JSON arrays can be inconsistent. XML does not inherently distinguish between a single element and a list of one element. Some converters might output a single JSON object instead of a JSON array containing one object, which can break consuming code expecting an array.
fix
Manually inspect generated JSON for array-like structures that might become single objects. Consider post-processing the JSON or using a library that allows explicit schema-driven conversion if strict array handling is required. The library might offer configuration for this, but it's a common XML-to-JSON challenge.
affects: All versions
gotchaXML is fundamentally text-based, so numerical, boolean, or null values in XML often get converted to strings in JSON. This can lead to type mismatches in downstream applications expecting specific JSON data types.
fix
Client-side type coercion is often necessary after conversion. If possible, define a JSON schema and validate/transform the output against it, or use custom parsing logic to detect and convert string representations of numbers/booleans/nulls to their correct JSON types.
affects: All versions
gotchaXML attributes and namespaces are handled according to the underlying conversion logic (likely `xmltodict`). By default, attributes might be prefixed (e.g., with '@') or even omitted, and namespaces may be stripped or embedded in the key names, which might not be intuitive or desired.
fix
Consult `xmltodict` documentation for its specific conventions on attributes and namespaces. If `xmltojson` provides configuration options to control this, utilize them. Otherwise, be prepared to clean or transform the JSON output to match the expected structure regarding attributes and namespaces.
affects: All versions
gotchaProviding malformed or empty XML input can lead to conversion failures, often manifesting as an 'ExecutionFailed' error (if using it in a larger pipeline or CLI context), or parsing exceptions.
fix
Always validate XML input for well-formedness before passing it to `xmltojson.parse()`. Implement robust error handling around conversion calls to catch and manage potential parsing exceptions.
affects: All versions
Errors
Common errors & fixes
ModuleNotFoundError: No module named 'xmltojson'
The 'xmltojson' Python package is not installed in the current environment.
fix
Run `pip install xmltojson` to install the library.
xml.etree.ElementTree.ParseError
The provided XML string or file content is not valid or well-formed XML, preventing the underlying ElementTree parser from processing it.
fix
Ensure the input XML is syntactically correct and well-formed according to XML standards before passing it to `xmltojson.convert()`.
TypeError: expected string or bytes-like object
The `xmltojson.convert()` function received an argument that is not a string, bytes, or a file-like object containing XML content.
fix
Pass a valid XML string, bytes, or an open file-like object to `xmltojson.convert()`.
AttributeError: module 'xmltojson' has no attribute 'to_json'
The module `xmltojson` does not expose a function or attribute named 'to_json'; the correct function for conversion is `convert`.
fix
Use `xmltojson.convert()` to perform the XML to JSON conversion.
xmltojson: error: argument -i/--input-file: Input file 'filename.xml' not found.
When using the command-line tool, the input file specified with `-i` or `--input-file` does not exist at the provided path.
fix
Provide the correct and complete path to an existing XML input file for the `-i` argument.
Upgrade
Version history
2.0.3latest on PyPI · released Oct 20, 2024
Audit
Dependencies
xmltodictrequiredUsed internally for parsing XML into a dictionary structure before conversion to JSON.
Agent activity
16 hits · last 30 days
node
14
OpenAI (training)
1
Resources
xmltojson — pip install xmltojson · libregistry