Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Where did json_dict go? #701

Closed
kittsville opened this issue Dec 28, 2016 · 11 comments
Closed

Where did json_dict go? #701

kittsville opened this issue Dec 28, 2016 · 11 comments

Comments

@kittsville
Copy link

I was wondering where the property json_dict of Reddit objects went? I can't seem to find any other way of retrieving the original JSON object returned by Reddit.

I've found many references to the property in past on this repo and StackOverflow but not what it was renamed to or why it was removed.

System Information

PRAW Version: 4.1.0
Python Version: 2.7
Operating System: Windows 10 (unfortunately)

@bboe
Copy link
Member

bboe commented Dec 28, 2016

Hi, great question. You're the first to ask about it. It's not currently available. Can you help me understand what your use case is for having it? It's probably something we could add back, I just want to understand the reasoning for it.

@kittsville
Copy link
Author

Hi,

Sure! I'm currently making Pyroon, a script which converts switch-a-roos into an interactive graph. The back-end can parse switch-a-roo chains and either save them or export them as Cytoscape.js formatted JSON. The front-end loads the formatted JSON and displays the graph.

json_dict would be used to save the switch-a-roo comments in case they were later deleted or mischievously edited to hyperlink elsewhere (common for Roos). I currently load deleted comments in PRAW with the PushShift API (a cache of Reddit) using praw.models.Comment(reddit, _data=pushshift_json). With access to json_dict I could save comments then later load them via the same means. Without relying on the PushShift API when I don't have to would reduce the risk of malicious edits (cached by PushShift) from permanently breaking the switch-a-roo chain.

tl;dr backing up switch-a-roos

@bboe
Copy link
Member

bboe commented Dec 28, 2016

Would using the pickle support PRAW has be a suitable alternative?

Here's an example:

def test_pickle(self):

@kittsville
Copy link
Author

I probably should have mentioned* I was also hoping to access the whole Reddit response from the front-end, to display information about roo comments when they're clicked on. While serialising the comment via pickle would probably be the best solution for the back-end it wouldn't be readable by the front.

I could always just write a helper for Pyroon to copy the 10 or so properties into a dict, something like:

def serialize(comment):
    return {
        'stickied': comment.stickied,
        'author': str(comment.author),
        'subreddit': str(comment.subreddit),
        # etc...
    }

*Sorry, my brain doesn't seem to be working much today

@bboe
Copy link
Member

bboe commented Dec 28, 2016

If you can manually json serialize just the properties you want I'd prefer that to exposing the underlying response for simplicity on PRAW's end. Please let me know if that's suitable.

@kittsville
Copy link
Author

kittsville commented Dec 28, 2016

That'd probably be best. I mostly didn't want to reinvent the wheel if the json_dict property still existed, just under a different name.

Plus it'll make my code more explicit, which is always a plus.

@bboe
Copy link
Member

bboe commented Dec 28, 2016

Great. I'm going to close this issue for now. Thanks for bringing it up.

@duhaime
Copy link

duhaime commented Jul 11, 2020

@bboe is it really not possible to access the full response from the Reddit API in JSON form? That seems like it should definitely be possible with this library...

@PythonCoderAS
Copy link
Contributor

It's completely possible. You just need to make a call to Reddit.request instead of Reddit.get, Reddit.post, and the other methods that objectify returned JSON data.

@duhaime
Copy link

duhaime commented Jul 12, 2020

Ah, perfect! Thanks very much @PythonCoderAS !

@johnwheeler
Copy link

import humps

def praw_object_to_dict(obj):
    data = {}
    for attr in dir(obj):
        if not attr.startswith("_") and not callable(getattr(obj, attr)):
            data[humps.camelize(attr)] = getattr(obj, attr)
    return data

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants