skip to Main Content

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


  1. It works straight forward using xmltodict.unparse() by surrounding the original dict with a root object

    import xmltodict
    
    original_json = {
        "mydicts": [
            {
                "id": 1,
                "name": "alice"
            },
            {
                "id": 2,
                "name": "bob"
            }
        ]
    }
    
    tmp_json = {}
    tmp_json['root'] = original_json
    
    xml_string = xmltodict.unparse(tmp_json)
    
    print(xml_string)
    
    new_json = xmltodict.parse(xml_string, xml_attribs=False)['root']
    
    print(new_json)
    

    Result

    <?xml version="1.0" encoding="utf-8"?>
    <root><mydicts><id>1</id><name>alice</name></mydicts><mydicts><id>2</id><name>bob</name></mydicts></root>
    {'mydicts': [{'id': '1', 'name': 'alice'}, {'id': '2', 'name': 'bob'}]}
    
    Login or Signup to reply.
  2. As addition to the answer from @LMC there is also a Modul json2xml:

    from json2xml import json2xml
    from json2xml.utils import readfromurl, readfromstring, readfromjson
    
    data = readfromstring('{"mydicts": [{"id": 1, "name": "alice"}]}')
    
    print(json2xml.Json2xml(data, wrapper="all", pretty=True).to_xml())
    

    Output:

    <?xml version="1.0" encoding="UTF-8"?>
    <all>
        <mydicts type="list">
            <item type="dict">
                <id type="int">1</id>
                <name type="str">alice</name>
            </item>
        </mydicts>
    </all>
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search