convert object to json string after function that needs python dict
Question: I have a python function/method that takes in a student and will show
profile... i realized i need to return context as a json string. how do i do
that?

    
    
    context["student"] = db.query_dict(student_profile_sql.format(student_id=self.kwargs["student_id"])
        )[0]
    
    appear(self.request, "Show profile", {
       "student_name": context["student"]["first_name...
    })
    
    return context  // i need to return context as json string how can i do that?
    

How can i return context as a json string?


Answer: Import the `json` library:

    
    
    import json
    

Then use `json.dumps`:

    
    
    return json.dumps(context)
    

From the [Python
documentation](https://docs.python.org/2/library/json.html#json.dumps):

> **`json.dumps(obj, ...)`**
>
> Serialize obj to a JSON formatted `str`

