I currently have an xml file in the following format:
<?xml version="1.0" encoding="UTF-8" ?>
<Garden>
<id>97</id>
<Flowers>
<id>98</id>
<Type>
<id>99</id>
<Level>
<id>100</id>
</Level>
</Type>
</Flowers>
</Garden>
I want to use xmltodict
to convert this xml to a dictionary and that is pretty simple to do. But there is a slight modification that I would like to do.
I would like to have my json be changed to something like this.
{
"Garden": {
"id": "97",
"state": "0",
"Flowers": {
"id": "98",
"state": "0",
"Type": {
"id": "99",
"state": "0",
"Level": {
"id": "100",
"state": "0"
}
}
}
}
}
I want to be able to add a default "state": "0"
for all the levels. I am really confused on how to do that. Any help would be really appreciated.
This is what I have as of now:
with open("gardenlist.xml", 'r') as file:
xmlString = file.read()
print(xmlString)
jsonString = json.dumps(xmltodict.parse(xmlString), indent=4)
This just prints the json but without the "state": "0"
values.