简体   繁体   中英

Convert XML to JSON with python, attributes of the father after the child nodes in the JSON

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?

I could format making use of the https://pypi.org/project/xmljson/ library, I modified the BadgerFish style

class BadgerFish(XMLData): # the convention was changed from the project the prefix _ and order the attributers of father nodes '''Converts between XML and data using the BadgerFish convention''' def init (self, **kwargs): super(BadgerFish, self). init (attr_prefix='_', text_content='$', **kwargs)

I changed the prefix of the attribute to "_"

by otherwise only changed the **# modify to put the father attributes to finish, how we can see in the code

def data(self, root):
        '''Convert etree.Element into a dictionary'''
        value = self.dict()
        children = [node for node in root if isinstance(node.tag, basestring)]
  
        if root.text and self.text_content is not None:
            text = root.text
            if text.strip():
                if self.simple_text and len(children) == len(root.attrib) == 0:
                    value = self._fromstring(text)
                else:
                    value[self.text_content] = self._fromstring(text)
        count = Counter(child.tag for child in children)
        for child in children:
            # if child.tag == "System_State_Entry": print(child.tag)
            if count[child.tag] == 1:
                value.update(self.data(child))
            else:
                result = value.setdefault(child.tag, self.list())
                result += self.data(child).values()
        # if simple_text, elements with no children nor attrs become '', not {}
        if isinstance(value, dict) and not value and self.simple_text:
            value = ''

        **# modify to put the father atributes to finish
        for attr, attrval in root.attrib.items():
            attr = attr if self.attr_prefix is None else self.attr_prefix + attr
            value[attr] = self._fromstring(attrval)**     
        return self.dict([(root.tag, value)])

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM