Open In App

Difference between Json and Dictionary in Python

Last Updated : 22 Dec, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

In Python, dictionaries and JSON (JavaScript Object Notation) both facilitate structured data representation, yet they diverge in usage and syntax. In this article, we will discuss the Definition of JSON and Dictionary and the difference between JSON and Dictionary in Python.

JSON vs Dictionary

Dictionaries, native to Python, are employed for in-memory data structures, allowing direct manipulation. In contrast, JSON serves as a standardized string-based format, vital for data exchange between systems.

Distinct in their data types, dictionaries offer in-memory flexibility, while JSON, with its string format, excels in interoperability. Below we will see differences in the form of a table.

What is JSON in Python?

JSON (Javascript Object Notation) is a standard format for transferring data as text over a network. JSON is a network-based data exchange and storage syntax. It makes extensive use of APIs and databases that are simple to read and understand for both humans and machines. Python includes a library called ‘json’ that may be used to work with JSON data. To use this feature, you must first import the JSON package into your Python code.

Python JSON holds data in the form of key-value pairs enclosed by curly brackets(), making it very similar to a Python dictionary. However, in this case, the JSON key is a string object with a mandatory double quotation mark. The value corresponding to the key, on the other hand, could be of any data type, such as string, integer, nested JSON, or any other sequence data type equivalent to an array.

Example 1: Simple Json Structure

In this example, a JSON string a is provided, representing data about a person. The json.loads() function is then used to parse the JSON string into a Python dictionary b. The code prints the value associated with the “city” key from the parsed dictionary, resulting in the output “Banglore.”

Python3




import json
 
# some JSON:
a = '{ "name":"Rahul", "age":21, "city":"Banglore"}'
# parse x:
 
b = json.loads(a)
print(b["city"])


Output

Banglore


Example 2: Nested Json Example

In this example, the variable nested_json holds a string representation of nested JSON data. The code uses print() to display the nested JSON string, showcasing its structured hierarchy with nested objects, such as “person” details and an embedded “address” with city and country attributes.

Python3




# Nested JSON String
nested_json = '{"person": {"name": "Alice", "age": 25, "address": {"city": "Wonderland", "country": "Fictional"}}}'
 
# Print the nested JSON string
print("Nested JSON String:", nested_json)


Output

Nested JSON String: {"person": {"name": "Alice", "age": 25, "address": {"city": "Wonderland", "country": "Fictional"}}}


What is Dictionary in Python?

Dictionary is a data type in Python that is used to store multiple rows of data in a single variable. The Python dictionary stores data values similarly to a visualize, which is not allowed by any other data type that retains only a single value as an element. A dictionary is an unordered and changeable collection of data components stored inside curly brackets() as key:value pairs. The colon(:) represents the key that corresponds to the value.

Dictionary values can be of any data type and can have duplicate values, however dictionary keys are unique and cannot be changed.

Example 1: Simple Dictionary Example

In this example, a dictionary named dict is defined with key-value pairs representing color, car, and flower. The code utilizes print() to display the entire dictionary, showcasing its structure and content.

Python3




dict = {
  "color": "blue",
  "car": "farari",
  "flower": "jasmine"
}
print(dict)


Output

{'color': 'blue', 'car': 'farari', 'flower': 'jasmine'}


Example 2: Nested Dictionary Example

In this example, a nested dictionary named nested_dict is created, representing information about a person with an embedded address. The code employs print() to showcase the entire nested structure, highlighting the hierarchical arrangement of data within the dictionary.

Python3




# Nested Dictionary
nested_dict = {"person": {"name": "Alice", "age": 25,
                          "address": {"city": "Wonderland", "country": "Fictional"}}}
print("Nested Dictionary:", nested_dict)


Output

Nested Dictionary: {'person': {'name': 'Alice', 'age': 25, 'address': {'city': 'Wonderland', 'country': 'Fictional'}}}


Difference between Json and Dictionary in Python

Json

Dictionary

JSON keys can only be strings.

The dictionary’s keys can be any hashable object.

The keys in JSON are ordered sequentially and can be repeated.

The keys in the dictionary cannot be repeated and must be distinct.

The keys in JSON have a default value of undefined.

There is no default value in dictionaries.

The values in a JSON file are accessed by using the “.” (dot) or “[]” operator.

The subscript operator is used to access the values in the dictionary. For example, if ‘dict’ = ‘A’:’123R’,’B’:’678S’, we can retrieve data related by simply calling dict[‘A’].

For the string object, we must use double quotation marks.

For string objects, we can use either a single or double quotation.

In JSON, the return object type is a’string’ object type.

The ‘dict’ object type is the return object type in a dictionary.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads