Rahul
Rahul

Reputation: 2100

Convert list with each element an object of class into json

I have a python list where each element in the list is the object of a class Summershum.Model.File:

message = [
<File(tar_file:appstream-glib-0.1.5.tar.xz, filename:/appstream-glib-0.1.5/config.guess, sha256:4a86808b907403ad6801c0661a4524dfa07c0b898a2cef6e7fa0cf82a09b9c53)>, 
<File(tar_file:totem-3.12.1.tar.xz, filename:/totem-3.12.1/build-aux/config.guess, sha256:4a86808b907403ad6801c0661a4524dfa07c0b898a2cef6e7fa0cf82a09b9c53)>,
<File(tar_file:gvfs-1.20.2.tar.xz, filename:/gvfs-1.20.2/config.guess, sha256:4a86808b907403ad6801c0661a4524dfa07c0b898a2cef6e7fa0cf82a09b9c53)>,
<File(tar_file:gnome-software-3.12.2.tar.xz, filename:/gnome-software-3.12.2/config.guess, sha256:4a86808b907403ad6801c0661a4524dfa07c0b898a2cef6e7fa0cf82a09b9c53)>,
<File(tar_file:gnome-packagekit-3.12.2.tar.xz, filename:/gnome-packagekit-3.12.2/config.guess, sha256:4a86808b907403ad6801c0661a4524dfa07c0b898a2cef6e7fa0cf82a09b9c53)>,
<File(tar_file:gnome-color-manager-3.12.2.tar.xz, filename:/gnome-color-manager-3.12.2/config.guess, sha256:4a86808b907403ad6801c0661a4524dfa07c0b898a2cef6e7fa0cf82a09b9c53)>,
<File(tar_file:gnome-chess-3.12.2.tar.xz, filename:/gnome-chess-3.12.2/config.guess, sha256:4a86808b907403ad6801c0661a4524dfa07c0b898a2cef6e7fa0cf82a09b9c53)>,
<File(tar_file:gnome-power-manager-3.12.2.tar.xz, filename:/gnome-power-manager-3.12.2/config.guess, sha256:4a86808b907403ad6801c0661a4524dfa07c0b898a2cef6e7fa0cf82a09b9c53)>,
<File(tar_file:evolution-mapi-3.12.2.tar.xz, filename:/evolution-mapi-3.12.2/config.guess, sha256:4a86808b907403ad6801c0661a4524dfa07c0b898a2cef6e7fa0cf82a09b9c53)>,
<File(tar_file:cockpit-0.7.tar.bz2, filename:/cockpit-0.7/tools/config.guess, sha256:4a86808b907403ad6801c0661a4524dfa07c0b898a2cef6e7fa0cf82a09b9c53)>,
<File(tar_file:xf86-video-freedreno-1.1.0.tar.bz2, filename:/xf86-video-freedreno-1.1.0/config.guess, sha256:4a86808b907403ad6801c0661a4524dfa07c0b898a2cef6e7fa0cf82a09b9c53)>, 
<File(tar_file:elfutils-0.159.tar.bz2, filename:/elfutils-0.159/config/config.guess, sha256:4a86808b907403ad6801c0661a4524dfa07c0b898a2cef6e7fa0cf82a09b9c53)>,
<File(tar_file:ibus-table-1.5.0.20140519.tar.gz, filename:/ibus-table-1.5.0.20140519/config.guess, sha256:4a86808b907403ad6801c0661a4524dfa07c0b898a2cef6e7fa0cf82a09b9c53)>, 
<File(tar_file:fence-agents-4.0.9.tar.xz, filename:/fence-agents-4.0.9/config.guess, sha256:4a86808b907403ad6801c0661a4524dfa07c0b898a2cef6e7fa0cf82a09b9c53)>
]

I am trying to convert this list into json and want to pass it to view for displaying (filename, sha1sum).

I was trying to convert the class object using "json" method: message = [msg.json() for msg in message] But

print message

gives me nothing empty list. What is the correct way of doing it?

Upvotes: 0

Views: 128

Answers (1)

Rob Watts
Rob Watts

Reputation: 7146

Based on the output of dir(message[0]) that you provided, the error is simple - _there is no __json__() method for a summershum.model.File object. So I'm guessing that the relevant section of your code looks like this:

try:
    ...
    message = [msg.__json__() for msg in message]
    print message
    ...
except Exception:
    # Some code, or just "pass"

Because there is no __json__() method, you should be getting an AttributeError. However, it sounds like you were not seeing an exception, which is why I assume there's a try...except block surrounding the code.

With no built-in method to convert the file contents to json, you'll have to use the built-in json module. For example:

>>> import json
>>> jsonString = '{"one":"two"}'
>>> jsonObj = json.loads(jsonString)
>>> jsonObj
{u'one': u'two'}

You'll need to call json.loads and give it the contexts of the file. I'm not sure exactly what you'll need to do that, but based on the available methods I would suggest seeing what the get and tar_file methods give you.

It's also possible that you're just trying to call __json__() too soon - the objects you get from get or tar_file might have a __json__() method.

Upvotes: 1

Related Questions