Skip to content

Examples

Dictionary to every text format

from lupaxa.data_converter import DataConverter

payload = {"name": "Lupaxa", "formats": ["json", "json5", "xml", "yaml", "toml"]}
converter = DataConverter(payload, data_type="dict")

print(converter.to_json())
print(converter.to_json5())
print(converter.to_xml())
print(converter.to_yaml())
print(converter.to_toml())

JSON string to XML and YAML

converter = DataConverter('{"id": 1, "ok": true}', data_type="json")
print(converter.to_xml())
print(converter.to_yaml())

JSON5 to TOML via the dict hub

converter = DataConverter("{ id: 1, ok: true, }", data_type="json5")
print(converter.to_toml())

YAML string to a dictionary

data = DataConverter.yaml_to_dict("name: Lupaxa\nactive: true\n")
print(data["name"])

XML string to JSON

print(DataConverter.xml_to_json("<root><id>1</id></root>"))
# {"id": "1"}

Dictionary to XML with a custom root

print(DataConverter.dict_to_xml({"id": 1}, root_tag="item"))