16

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 ?

zondo
  • 19,901
  • 8
  • 44
  • 83
user1542557
  • 181
  • 1
  • 1
  • 4
  • 6
    `.json()` returns a dictionary. It is no special object. If a dictionary let `.city` work, what would it do for numerical keys? When you said `dictionary.copy`, would you be asking for the `copy` method of the dictionary, or `dictionary['copy']`? – zondo Aug 11 '16 at 00:16
  • 7
    Python isn't Javascript. In Javascript, `thing.x` is defined as shorthand for `thing["x"]`; in Python, those are two completely different things. – user2357112 Aug 11 '16 at 00:20

4 Answers4

14

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.

2Cubed
  • 3,401
  • 7
  • 23
  • 40
  • 1
    Thank you, now it's more clearly. I was under misapprehension because in Python Dictionary data type is subtype of Object and thought that I can use it in same way. – user1542557 Aug 11 '16 at 00:39
3

Idk know if Im needed here but still I will try.

Note: Everything is based on my current knowledge so pardon me if this is going wrong anywhere, and please correct me for the community to keep growing

My answer actually contains fundamentals as to how they work and why? So if u needed that then here it is- First of all there are 2 types of JSON:

  1. json data
  2. json string

json data as we know is exactly like dictionary in python. That means they work on hash maps. But we convert it to string before transferring the json data to somewhere else through the networks.

So it will look something like this: json_data = {"data":"This will original"}

After conversion it will look like this : '{"data":"This will original"}'

You will notice that the json_data has been converted to string and is now being written within quotation. You can also check the data type in python through, type(json_data). The reason why we do this is because the cost of string is much less than that of the actual objects. And this is much easier to do. So now when this data will be transmitted to some other language(like python) then they will receive a json string. And now they have to parse these to objects to be able to use the data inside it. Otherwise they are just simple strings. So now u need to convert it back to json data. And in python json data or java script object is equivalent to dictionary. That's why we convert the string to dict. Other languages must be having different names for their dictionary type data structure then it will convert the string to those type of data structure. In python to be able to convert from string to json and json to string. That class must have json serializers to know what and how to convert.

Hope this helps even if little. And if there are any doubts I'll be happy to answer.

Miguel Conde
  • 813
  • 10
  • 22
Amit
  • 73
  • 5
2

JSON stands for JS Object Notation. So it's a notation, a format - merely a set of rules defining how to represent data in text format.
Python Dictionary is a class representing hash table data structure in Python. It just happens to look like JSON; it's much more than that.
Note there structure is pretty much the same.
You can check out more here.

Wangwe
  • 171
  • 1
  • 6
-1

From the documentation:

json.loads(s, *, cls=None, object_hook=None, parse_float=None, parse_int=None, parse_constant=None, object_pairs_hook=None, **kw)
Deserialize s (a str, bytes or bytearray instance containing a JSON document) to a Python object using this conversion table.

This conversion table shows how JSON is mapped to Python. This other conversion table shows how Python dicts are mapped to JSON.

NFB
  • 642
  • 8
  • 26