'How to convert JSON to XML using python?

i need to convert a JSON file

{
"Order1/variable1.py": {
    "Order1/variable1.py": null,
    "Order1/variable2.py": 7,
    "Order1/variable3.py": 23,
    "Order2/variable4.py": 61,
    "Order2/variable5.py": 0,
},
"Order1/variable2.py": {
    "Order1/variable1.py": 100,
    "Order1/variable2.py": null,
    "Order1/variable3.py": 35,
    "Order2/variable4.py": 13,
    "Order2/variable5.py": 0,
},
"Order1/variable3.py": {
    "Order1/variable1.py": 18,
    "Order1/variable2.py": 24,
    "Order1/variable3.py": null,
    "Order2/variable4.py": 0,
    "Order2/variable5.py": 0,
},
"Order2/variable4.py": {
    "Order1/variable1.py": 6,
    "Order1/variable2.py": 72,
    "Order1/variable3.py": 39,
    "Order2/variable4.py": null,
    "Order2/variable5.py": 0,
},
"Order2/variable5.py": {
    "Order1/variable1.py": 0,
    "Order1/variable2.py": 71,
    "Order1/variable3.py": 88,
    "Order2/variable4.py": 69,
    "Order2/variable5.py": null,
},}

into an XML file that must look like this

<?xml version="1.0" encoding="utf-8"?>
<model elementCount="7" relationCount="20">
  <elements>
    <element id="2" order="2" name="Order1" type="Order" expanded="True" />
    <element id="3" order="3" name="variable1" type="Variable" expanded="False" parent="2" />
    <element id="4" order="4" name="variable2" type="Variable" expanded="False" parent="2" />
    <element id="5" order="5" name="variable3" type="Variable" expanded="False" parent="2" />
    <element id="6" order="6" name="Order2" type="Order" expanded="True" />
    <element id="7" order="7" name="variable4" type="Variable" expanded="False" parent="6" />
    <element id="8" order="8" name="variable5" type="Variable" expanded="False" parent="6" />

  </elements>
  <relations>
    <relation id="1" from="3" to="4" type="" weight="7" />
    <relation id="2" from="3" to="5" type="" weight="23" />
    <relation id="3" from="3" to="7" type="" weight="61" />
    <relation id="4" from="3" to="8" type="" weight="0" />
    <relation id="5" from="4" to="3" type="" weight="100" />
    <relation id="6" from="4" to="5" type="" weight="35" />
    <relation id="7" from="4" to="7" type="" weight="13" />
    <relation id="8" from="4" to="8" type="" weight="0" />
    <relation id="9" from="5" to="3" type="" weight="18" />
    <relation id="10" from="5" to="4" type="" weight="24" />
    <relation id="11" from="5" to="7" type="" weight="0" />
    <relation id="12" from="5" to="8" type="" weight="0" />
    <relation id="13" from="7" to="3" type="" weight="6" />
    <relation id="14" from="7" to="4" type="" weight="72" />
    <relation id="15" from="7" to="5" type="" weight="39" />
    <relation id="16" from="7" to="8" type="" weight="0" />
    <relation id="17" from="8" to="3" type="" weight="0" />
    <relation id="18" from="8" to="4" type="" weight="71" />
    <relation id="19" from="8" to="5" type="" weight="88" />
    <relation id="20" from="8" to="7" type="" weight="69" />
  </relations>
</model>

The JSON file will always be in that format since it comes from another tool, but it may contain more elements (Orders or variables) and therefore each variable would have more rows (e.g., the element Order1/variable1.py may contain some more value like "Order3/variable6.py": 25,).

The XML file also must to be in the format from this example, but the "relations" with weight = 0 may be ignored.

Since i am very new to this topic, i have no idea how to convert it and would be very grateful for any advices.

*I must realize it using python



Solution 1:[1]

I made this script to convert your JSON to XML. I haven't tried some particular cases of JSON, but you can still use it as a base.

import json


with open('json_data.json', 'r') as f:
    data = json.load(f)

xml_dict = {}
for key in data:
    order, variable = key.split("/")
    variable = variable.replace(".py", "")
    if order not in xml_dict:
        xml_dict[order] = [variable]
    else:
        xml_dict[order].append(variable)

xml = '<elements>'
object_id = []
for key in xml_dict:
    order_index = len(object_id) + 2
    xml += f'<element id="{order_index}" order="{order_index}" name="{key}" type="Order" expanded="True" />'
    object_id.append(key)
    for variable in xml_dict[key]:
        xml += f'<element id="{len(object_id) + 2}" order="{len(object_id) + 2}" name="{variable}" type="Variable" expanded="False" parent="{order_index}" />'
        object_id.append(variable)
xml += '</elements>'

xml += '<relations>'
index = 0
for key in data:
    index_from = object_id.index(key.split('/')[1].replace('.py', '')) + 2
    for relation in data[key]:
        if data[key][relation] is not None:
            index_to = object_id.index(relation.split('/')[1].replace('.py', '')) + 2
            xml += f'<relation id="{index + 1}" from="{index_from}" to="{index_to}" type="" weight="{data[key][relation]}" />'
            index += 1
xml += '</relations>'

xml = f'<?xml version="1.0" encoding="utf-8"?><model elementCount="{len(object_id)}" relationCount="{index}">' + xml + '</model>'

with open('xml_data.xml', 'w') as f:
    f.write(xml)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Cubix48