I need convert one file XML to JSON with python, but I need put the attributes of the father after the child nodes in the JSON
My current code is like that.
def generate_json(self, event=None):
# opening the xml file
with open(self.fn ,"r") as xmlfileObj:
data_dict = lib.xmltodict.parse(xmlfileObj.read(),attr_prefix='_')
xmlfileObj.close()
jsonObj= json.dumps(data_dict, sort_keys=False)
restored = json.loads(jsonObj)
#storing json data to json file
with open("data.json", "w") as jsonfileObj:
jsonfileObj.write(jsonObj)
jsonfileObj.close()
and I need this;
{
"datasetVersion": {
"metadataBlocks": {
"citation": {
"fields": [
{
"value": "Darwin's Finches",
"typeClass": "primitive",
"multiple": false,
"typeName": "title"
}
],
"displayName": "Citation Metadata"
}
}
}
}
in place of:
{
"datasetVersion": {
"metadataBlocks": {
"citation": {
"displayName": "Citation Metadata",
"fields": [
{
"value": "Darwin's Finches",
"typeClass": "primitive",
"multiple": false,
"typeName": "title"
}
]
}
}
}
}
No in alphabetic order changing sort_keys=False, I need only to change the attributes of node father to the final.
on some website make how I need: https://www.convertjson.com/xml-to-json.htm
and another no how:
http://www.utilities-online.info/xmltojson/#.X_crINhKiUk
can somebody help me?