Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Single element array #26

Open
numjcp opened this issue Oct 11, 2017 · 5 comments
Open

Single element array #26

numjcp opened this issue Oct 11, 2017 · 5 comments

Comments

@numjcp
Copy link

numjcp commented Oct 11, 2017

Hello,
great job with this python module !!

However, i came across a simple problem.
Say i wish to convert the following xml to json along yahoo convention

i get
{"commandes": {"commande": [{}, {}]}}
which is ok for me.

But if i convert the same xml with only one object

i get
{"commandes": {"commande": {}}}
which is Good but not the behavior I expect. I'd like to get the single element within an array as if several objects where present. That is to say :
{"commandes": {"commande": [{}]}}

Thanks

@numjcp
Copy link
Author

numjcp commented Oct 11, 2017

<commandes><commande/><commande/></commandes>
and
<commandes><commande/></commandes>

are the source xml's

@numjcp
Copy link
Author

numjcp commented Oct 11, 2017

Here's a quick fix at first glance.

(How) would you like to integrate this behaviour ?

def data(self, root):
        '''Convert etree.Element into a dictionary'''
        value = self.dict()
        children = [node for node in root if isinstance(node.tag, basestring)]
        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)
        if root.text and self.text_content is not None:
            text = root.text.strip()
            if text:
                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 count[child.tag] == 1:
            #    value.update(self.data(child))
            #else:
            result = value.setdefault(child.tag, self.list())
            result += self.data(child).values()
        return self.dict([(root.tag, value)])

@wyleung
Copy link

wyleung commented Dec 6, 2017

This is exactly the same issue i'm facing.

I think, this problem is more related to the source file or the need for a xsd where the expected elements are described

@javadev
Copy link

javadev commented Nov 10, 2018

{
   "commandes": {
      "commande": [
         {
         },
         {
         }
      ]
   }
} 

may be converted to xml

<?xml version="1.0" encoding="UTF-8"?>
<commandes>
  <commande></commande>
  <commande></commande>
</commandes>

@DanielKaupp
Copy link

I'm having the same issue with the Parker Convention -> No problem here, as how I found out, Parker is specified that way. There is though another convention 'Spark' that is similar to Parker, but treats single entries as lists.

http://wiki.open311.org/JSON_and_XML_Conversion/

Perhaps someone could also provide 'Spark' Convention as an extension to xmljson?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants