vinitkumar/json2xml

TypeError: 'bool' object does not support item assignment

WolfgangFahl opened this issue · 3 comments

Describe the bug

  File "../Library/Python/3.8/lib/python/site-packages/dicttoxml.py", line 333, in convert_kv
    attr['type'] = get_xml_type(val)
TypeError: 'bool' object does not support item assignment

To Reproduce

 def testJson2XML(self):
        ''' test Json  XML conversion '''
        jsonText='{"count": 1, "events": [{"acronym": "EuroPar 2020", "city": "Warsaw", "country": "Poland", "creation_date": "2020-02-27T14:44:52+00:00", "end_date": "2020-08-28T00:00:00+00:00", "event": "EuroPar 2020", "foundBy": "EuroPar 2020", "homepage": "https://2020.euro-par.org/", "modification_date": "2020-02-27T14:44:52+00:00", "series": "EuroPar", "source": "OPEN RESEARCH", "start_date": "2020-08-24T00:00:00+00:00", "title": "International European Conference on Parallel and Distributed Computing", "url": "https://www.openresearch.org/wiki/EuroPar 2020"}]}'
        xml=json2xml.Json2xml(jsonText).to_xml()  
        print(xml)    

Expected behavior
should not fail

Desktop (please complete the following information):

  • OS: MacOS 10.13.6

Hi @WolfgangFahl. Thanks for reporting this issue. I just tried to reproduce this issue. Looks like your code is either using an old version of the documentation.

First of all, make sure you are using a more recent version of json2xml. The latest one now is json2xml (3.4.1) - Simple Python Library to convert JSON to XML

So either install it directly like this: pip install json2xml==3.4.1 or upgrade pip install --upgrade json2xml. Then verify if you have the latest json2xml before proceeding any further. The output should be like this:

pip freeze | grep json2xml
json2xml==3.4.1

Now, looking at your example, this is how the correct code would look like:

from json2xml import json2xml 
from json2xml.utils import readfromurl, readfromstring, readfromjson 

# we have a method to read the json string now
data =   readfromstring('{"count": 1, "events": [{"acronym": "EuroPar 2020", "city": "Warsaw", "country": "Poland", "creation_date": "2020-02-27T14:44:52+00:00", "end_date": "2020-08-28T00:00:00+00:00", "event": "EuroPar 2020", "foundBy": "EuroPar 2020", "homepage": "https://2020.euro-par.org/", "modification_date": "2020-02-27T14:44:52+00:00", "series": "EuroPar", "source": "OPEN RESEARCH", "start_date": "2020-08-24T00:00:00+00:00", "title": "International European Conference on Parallel and Distributed Computing", "url": "https://www.openresearch.org/wiki/EuroPar 2020"}]}')

xml = json2xml.Json2xml(data).to_xml() 
print(xml)

Which returns:

<?xml version="1.0" ?>
<all>
	<count type="int">1</count>
	<events type="list">
		<item type="dict">
			<acronym type="str">EuroPar 2020</acronym>
			<city type="str">Warsaw</city>
			<country type="str">Poland</country>
			<creation_date type="str">2020-02-27T14:44:52+00:00</creation_date>
			<end_date type="str">2020-08-28T00:00:00+00:00</end_date>
			<event type="str">EuroPar 2020</event>
			<foundBy type="str">EuroPar 2020</foundBy>
			<homepage type="str">https://2020.euro-par.org/</homepage>
			<modification_date type="str">2020-02-27T14:44:52+00:00</modification_date>
			<series type="str">EuroPar</series>
			<source type="str">OPEN RESEARCH</source>
			<start_date type="str">2020-08-24T00:00:00+00:00</start_date>
			<title type="str">International European Conference on Parallel and Distributed Computing</title>
			<url type="str">https://www.openresearch.org/wiki/EuroPar 2020</url>
		</item>
	</events>
</all>

Thx. I also fixed the problem with the upstream dict2xml library which was the initial culprit of the error message.
i dont really need json2xml since i can get access to the original objects and their dicts ...

See
http://ptp.bitplan.com/parse?titles=EuroPar+2020&examples=example1&format=xml

from dicttoxml import dicttoxml
from xml.dom.minidom import parseString

def asXml(self,result,pretty=True):
        ''' convert result to XML'''
        events=[]
        for title in result:
            for event in title.events:
                events.append(event.__dict__)
        item_name = lambda x: "event"        
        xml=dicttoxml(events, custom_root='events',item_func=item_name, attr_type=False)
        if pretty:
            dom=parseString(xml)
            prettyXml=dom.toprettyxml()
        else:
            prettyXml=xml    
        return prettyXml

result

<events>
<event>
<foundBy>EuroPar 2020</foundBy>
<homepage>https://2020.euro-par.org/</homepage>
<event>EuroPar 2020</event>
<series>EuroPar</series>
<acronym>EuroPar 2020</acronym>
<title>
International European Conference on Parallel and Distributed Computing
</title>
<city>Warsaw</city>
<country>Poland</country>
<start_date>2020-08-24T00:00:00</start_date>
<end_date>2020-08-28T00:00:00</end_date>
<creation_date>2020-02-27T14:44:52</creation_date>
<modification_date>2020-02-27T14:44:52</modification_date>
<url>https://www.openresearch.org/wiki/EuroPar 2020</url>
<source>OPEN RESEARCH</source>
</event>
</events>