I have a need to convert some json to xml and then back to json ideally preserving the structure. I’ve gotten quite close but I can’t find a way to use dicttoxml/xmltodict to not end up with each list of dictionaries being a dictionary with the key ‘item’ and the original dictionary it’s value.
e.g.
{'mydicts': [{'id': 1, 'name': 'alice'}]}
becomes:
<?xml version="1.0" encoding="UTF-8" ?><root><mydicts><item><id>1</id><name>alice</name></item></mydicts></root>
ends up as:
{'mydicts': [{'item': {'id': '1', 'name': 'alice'}}]}
I had to use force_list as well originally. My last resort will be some post-processing code to make this change but I’d prefer to avoid that. Here is some sample code of what I have now:
import xmltodict
import dicttoxml
original_json = {
"mydicts": [
{
"id": 1,
"name": "alice"
}
]
}
xml_string = dicttoxml.dicttoxml(original_json, attr_type=False).decode()
new_json = xmltodict.parse(xml_string, xml_attribs=False, force_list={'mydicts'})['root']
print(new_json)
Other solutions are perfectly fine as well.
2
Answers
It works straight forward using
xmltodict.unparse()
by surrounding the original dict with a root objectResult
As addition to the answer from @LMC there is also a Modul json2xml:
Output: