Need explanation difference between json and dict in Python

user1542557 picture user1542557 · Aug 11, 2016 · Viewed 7.2k times · Source

I am just curious to understand JSON and Dict in Python more deeply.

I have a JSON response from a server like this:

`{"city":"Mississauga","country":"Canada","countryCode":"CA"}`

And I want to work with it as a dictionary. For this, I use .json() function. Why can I get data by using res.json()['city'], but cannot do it with req.json().city ?

Answer

2Cubed picture 2Cubed · Aug 11, 2016

In Python, dictionary values are not accessible using the my_dict.key syntax. This is reserved for attributes of the dict class, such as dict.get and dict.update. Dictionary values are only accessible via the my_dict[key] syntax.