Skip to content

Error while converting JSON2Obj for list of string #2

Open

Description

@raghavlane

while converting a list of string from json to object error pops up .
Please Find below the sample code (modified to have json to object) shared in the pyconvert git

import pyconvert.pyconv
class Band(object):
name_band = str
year_band = int
class Album(object):
band_album = Band
name_album = str
year_album = int
song = [str]
band = Band()
band.name_band = "Led Zeppelin"
band.year_band = 1968
album = Album()
album.band_album = band
album.name_album = "House of Holy"
album.year_album = 1973
album.song = list()
album.song.append("The Song Remains the Same")
album.song.append("The Rain Song")
album.song.append("Over the Hills and Far Away")
album.song.append("The Crunge")
album.song.append("Dancing Days")
album.song.append("D'yer Mak'er")
album.song.append("No Quarter")
album.song.append("The Ocean")
print("#####################")
json_doc = pyconvert.pyconv.convert2JSON(album)
print(json_doc)
print("#####################################")
object= pyconvert.pyconv.convertJSON2OBJ(Album,json_doc)
print(object)

The ERROR is:
Traceback (most recent call last):
File "sample.py", line 32, in
aaa= pyconvert.pyconv.convertJSON2OBJ(Album,json_doc)
File "/usr/lib/python3.4/site-packages/pyconvert/pyconv.py", line 322, in convertJSON2OBJ
print(isinstance(cls.dict[key],list))
KeyError: 'songs'

Activity

  1. raghavlane commented on Nov 29, 2016

    @raghavlane
    Author

    Hi,

    I have made few modification to handle this bug.I have been using my modified version for testing over the last few months.
    Please Find Below the modifications done on convertJSON2OBJ method in pyconv.py:

    def convertJSON2OBJ(cls, json_doc):
    """ function that convert an json in a python object
    parameters:
    cls, is the class that represents to the json.
    doc, is the json documment

    		example:
    			class Person:
    				name = str
    				age = int
    
    			{
    				"name" : "Steve"
    				"age"  : 41
    			}
    
    	returns:
    		python oject
    """
    attrs = {}
    if isinstance(json_doc,list):
    	l = []
    	for a in json_doc:
    		b = convertJSON2OBJ(cls, a)
    		l.append(b)
    	return l
    else:
    	for key in json_doc.keys():
    		value = json_doc[key]
    		if not isinstance(value,list) and not isinstance(value,dict):
    			type_conv = type(value)
    			attrs[key] = type_conv(value)
    		elif isinstance(value,dict) and not isinstance(cls.__dict__[key],list):
    			attrs[key] = convertJSON2OBJ(cls.__dict__[key],value)
    		elif isinstance(value,list) and not isinstance(value,dict):
    			type_conv = type(value)
    			attrs[key] = type_conv(value)
    		else:
    			l = []
    			for a in value:
    				b = convertJSON2OBJ(cls.__dict__[key][0], a)
    				l.append(b)
    			attrs[key] = l
    	c = cls()
    	c.__dict__ = attrs
    	return c
    

    Please let me know if this change would suffice all other requirements.

    Regards,
    Raghav

  2. rancavil commented on Nov 29, 2016

    @rancavil
    Owner

    Nice. I gonna test...please meanwhile you can make a pull request.

    Thanks.

  3. raghavlane commented on Dec 20, 2016

    @raghavlane
    Author
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions