Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Need explanation difference between json and dict in Python

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 ?

like image 206
user1542557 Avatar asked Aug 11 '16 00:08

user1542557


People also ask

What is difference between dict and JSON in Python?

As discussed above, JSON is a data format, and a dictionary in python is a data structure. If you wish to exchange data for different processes, you should use JSON format to serialize your python dictionary.

Is JSON just a Python dictionary?

JavaScript Object Notation (JSON) is a standardized format commonly used to transfer data as text that can be sent over a network. It's used by lots of APIs and Databases, and it's easy for both humans and machines to read. JSON represents objects as name/value pairs, just like a Python dictionary.

Is a JSON file just a dictionary?

JSON (JavaScript Object Notation) is one of the most popular formats used to transfer data between websites and programs. It's a human-readable format, yet structured in a familiar style that you'll recognize ... it's essentially a mix of dictionaries and lists!

Does JSON () return a dict?

json. loads take a string as input and returns a dictionary as output.


2 Answers

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.

like image 73
Amit Avatar answered Sep 28 '22 16:09

Amit


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.

like image 37
2Cubed Avatar answered Sep 28 '22 16:09

2Cubed