CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/pypi-xmljson

Converts XML into JSON/Python dicts/arrays and vice-versa using multiple conventions

Overview
Eval results
Files

yahoo.mddocs/

Yahoo Convention

Yahoo convention uses 'content' for text content and disables automatic string parsing, keeping all values as strings unless explicitly converted. It follows the format used by Yahoo's JSON APIs.

Capabilities

Yahoo Class

Creates a Yahoo converter with 'content' for text and string preservation.

class Yahoo(XMLData):
    def __init__(self, **kwargs):
        """
        Initialize Yahoo converter.
        
        Parameters:
        - **kwargs: Additional XMLData parameters (xml_fromstring, xml_tostring,
                   element, dict_type, list_type, simple_text, invalid_tags)
        
        Note: xml_fromstring defaults to False (no automatic type conversion)
        """

Data to XML Conversion

Converts Python dictionaries and lists to XML elements using Yahoo convention.

def etree(self, data, root=None):
    """
    Convert data structure into a list of etree.Element.
    
    Parameters:
    - data: dict or list, data structure to convert
    - root: Element, optional root element to append to
    
    Returns:
    List of etree.Element objects or modified root element
    """

XML to Data Conversion

Converts XML elements to Python dictionaries using Yahoo convention.

def data(self, root):
    """
    Convert etree.Element into a dictionary.
    
    Parameters:
    - root: Element, XML element to convert
    
    Returns:
    dict: Dictionary representation using Yahoo convention
    """

Usage Examples:

from xmljson import yahoo
from xml.etree.ElementTree import fromstring, tostring
import json

# XML to data conversion (strings preserved)
xml_string = '<item id="123"><title>Sample</title><count>42</count><active>true</active></item>'
xml_element = fromstring(xml_string)
data = yahoo.data(xml_element)
print(json.dumps(data))
# Output: {"item": {"id": "123", "title": "Sample", "count": "42", "active": "true"}}

# Data to XML conversion
data = {
    'item': {
        'id': '123',
        'title': 'Sample Title',
        'content': 'This is the content'
    }
}
elements = yahoo.etree(data)
print(tostring(elements[0]).decode())
# Output: <item id="123"><title>Sample Title</title>This is the content</item>

Pre-configured Instance

yahoo: Yahoo

A pre-configured Yahoo instance available at module level for immediate use.

Convention Rules

  • Text Content: Stored in 'content' key when mixed with attributes/children
  • Simple Text: Elements with only text content become string values directly
  • Attributes: Added as direct properties alongside content
  • No Type Conversion: All values remain as strings (xml_fromstring=False)
  • Arrays: Multiple elements with same name become arrays
  • String Preservation: Numbers, booleans remain as string representations

Use Cases

  • Legacy APIs: When working with older Yahoo-style JSON APIs
  • String Fidelity: When exact string representation must be preserved
  • No Type Inference: When automatic type conversion is undesirable
  • Simple Structures: XML with minimal nesting and string-based data

String Preservation Example

The Yahoo convention's key feature is string preservation:

from xmljson import yahoo, badgerfish
from xml.etree.ElementTree import fromstring
import json

xml_string = '<data><number>42</number><flag>true</flag><text>hello</text></data>'
xml_element = fromstring(xml_string)

# Yahoo preserves strings
yahoo_data = yahoo.data(xml_element)
print("Yahoo:", json.dumps(yahoo_data))
# Output: Yahoo: {"data": {"number": "42", "flag": "true", "text": "hello"}}

# BadgerFish converts types
bf_data = badgerfish.data(xml_element)  
print("BadgerFish:", json.dumps(bf_data))
# Output: BadgerFish: {"data": {"number": {"$": 42}, "flag": {"$": true}, "text": {"$": "hello"}}}

Limitations

  • No Type Inference: Manual conversion required for numbers/booleans
  • Content Key Conflicts: 'content' key name may conflict with element names
  • Limited Adoption: Less commonly used than other conventions

Install with Tessl CLI

npx tessl i tessl/pypi-xmljson

docs

abdera.md

badgerfish.md

cli.md

cobra.md

gdata.md

index.md

parker.md

yahoo.md

tile.json