pedro
pedro

Reputation: 775

List of lists of objects to JSON with Python

I have a List with multi lists of objects:

ob = Obj()
for p in l:
    list_url_commune = scrapingCities(base_url, p)
    for u in list_url_commune:
        list_name = scaping_creches(base_url, u, number_page)
        list_dep.append(list_name)

 json_string = json.dumps([ob.__dict__ for ob in list_dep])
 print json_string

list_dep is the list with all the lists with Objects

Then, I would like to convert all the Objects in only one JSON.

In my case, I have an error with json.dumps([ob.__dict__ for ob in list_dep])

(notice: it works fine if I have only one list but here it is a List of lists)

AttributeError: 'list' object has no attribute '__dict__'

Thanks for your help !

Pedro

Upvotes: 0

Views: 3798

Answers (1)

Remco Haszing
Remco Haszing

Reputation: 7829

Just like you noticed yourself, it works fine with a list of objects, but not with a list of lists of objects. You need to go deeper:

json.dumps([[ob.__dict__ for ob in lst] for lst in list_dep])

Upvotes: 2

Related Questions