Alexander Milano Manias Alexander Milano Manias - 10 months ago 51
JSON Question

from attribute in json on python

I called an url that returns me a json object with an attribute called

. So when i want to get this in python i try:


$ python
File "", line 63
print comment.from.username
SyntaxError: invalid syntax

Answer Source

from is a python keyword and should not be used as an attribute.

Lucky for you, when using the json library that comes with python, JSON objects become dictionaries instead:


For situations where you actually do have a object with a python keyword used as an attribute, you'd have to resort to using the getattr() function instead:

frm = getattr(object, 'from')