Skip to content

Instantly share code, notes, and snippets.

@jarhill0
Created April 30, 2019 03:55
Show Gist options
  • Save jarhill0/6e6495706252d52573950c3820f533b0 to your computer and use it in GitHub Desktop.
Save jarhill0/6e6495706252d52573950c3820f533b0 to your computer and use it in GitHub Desktop.
Sample code for converting a PRAW Comment to JSON
import praw
from json import dumps, JSONEncoder
reddit = praw.Reddit(username='',
password='',
client_id='',
client_secret='',
user_agent='')
comment = reddit.comment('em3rygg')
print(comment.body) # to fetch the comment
class PRAWJSONEncoder(JSONEncoder):
"""Class to encode PRAW objects to JSON."""
def default(self, obj):
if isinstance(obj, praw.models.base.PRAWBase):
obj_dict = {}
for key, value in obj.__dict__.items():
if not key.startswith('_'):
obj_dict[key] = value
return obj_dict
else:
return JSONEncoder.default(self, obj)
print(dumps(comment, cls=PRAWJSONEncoder))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment