Printing json results in python
Question: I'm using two files for this project. Snapchat.py and test.py

Snapchat.py contains the following (showing the important part):

    
    
    def add_friend(self, username):
        """Add user as friend
        Returns JSON response.
        Expected messages:
            Success: '{username} is now your friend!'
            Pending: '{username} is private. Friend request sent.'
            Failure: 'Sorry! Couldn't find {username}'
        :param username: Username to add as a friend
        """
        r = self._request('friend', {
            'action': 'add',
            'friend': username,
            'username': self.username
        })
        return r.json()
    

The file test.py, that I am currently working on has the following code:

    
    
    #including Snapchat in test.py
    from snapchat import Snapchat
    
    s = Snapchat()
    
    username = raw_input('Enter your username: ')
    password = raw_input('Enter your password: ')
    friend = raw_input('Enter friend name: ')
    s.login(username, password)
    
    s.add_friend(friend)
    

The important part here is:

    
    
        Returns JSON response.
        Expected messages:
            Success: '{username} is now your friend!'
            Pending: '{username} is private. Friend request sent.'
            Failure: 'Sorry! Couldn't find {username}'
    

I want that response printed at the end of the test.py file in the command
shell.

Though I have no clue on how to do this, I have tried importing it in the
test.py file and printing it, but not working.


Answer: Please note that the Messages you pasted in your question will infact be
returned by the add_friend method, so you don't need to do anything.

Simply print them like;

    
    
    #First you need to see what is the response for your login call
    login_response = s.login(username, password)
    print str(login_response)
    
    #if you wish to access some value in this response
    value = login_response.get("the_value", {the default value})
    
    if value: #incase you want to make sure you logged in correctly   
        json_response = s.add_friend(friend)
        print str(json_response)
    

