Python JSON

Python Tutorial

🐍 Python JSON – Complete Tutorial

JSON (JavaScript Object Notation) is a lightweight data format used to store and exchange data between systems.
In Python, JSON is commonly used in APIs, web applications, configuration files, and data exchange.


1️⃣ What is JavaScript Object Notation?

JSON stores data in key–value pairs, similar to Python dictionaries.

Example JavaScript Object Notation

{
"name": "Amit",
"age": 25,
"city": "Kolkata"
}

📌 JSON is:

  • Text-based

  • Language-independent

  • Easy to read & write


2️⃣ Python JSON Module

Python provides a built-in module called json.

import json

3️⃣ Python Object → JSON (json.dumps) ⭐

Convert Python data into JSON string.


 

Output

{"name": "Amit", "age": 25, "is_student": false}

📌 Python False → JSON false


4️⃣ JavaScript Object Notation → Python Object (json.loads) ⭐

Convert JavaScript Object Notation string into Python object.


 

Output

{'name': 'Amit', 'age': 25}
<class 'dict'>

5️⃣ Write JSON to File (json.dump) ⭐


 

✔ Creates a JSON file


6️⃣ Read JSON from File (json.load) ⭐


 


7️⃣ Pretty Print JSON ⭐

Make JSON readable.



8️⃣ Python ↔ JavaScript Object Notation Data Type Mapping ⭐

Python JSON
dict object
list array
str string
int / float number
True true
False false
None null

9️⃣ JSON with Lists ⭐


 


🔟 Access Data in Python



1️⃣1️⃣ Handling JSON Decode Errors ⭐


✔ Very important in APIs


1️⃣2️⃣ JSON in Real-Life Applications ⭐

✔ REST APIs
✔ Web services
✔ Configuration files
✔ Mobile apps
✔ Data exchange


❌ Common Mistakes

❌ Using single quotes in JSON
❌ Forgetting to import json
❌ Confusing dump vs dumps
❌ Confusing load vs loads


📌 Interview Questions & MCQs

Q1. Which module is used for JavaScript Object Notation in Python?

A) js
B) json
C) pyjson
D) object

Answer: B


Q2. Convert Python object to JavaScript Object Notation string?

A) json.load()
B) json.dump()
C) json.dumps()
D) json.loads()

Answer: C


Q3. Convert JavaScript Object Notation string to Python object?

A) json.dumps()
B) json.loads()
C) json.load()
D) json.dump()

Answer: B


Q4. JavaScript Object Notationnull becomes in Python?

A) 0
B) False
C) None
D) Empty

Answer: C


Q5. Is JSON language-dependent?

A) Yes
B) No

Answer: B


✅ Summary

  • JSON is used for data exchange

  • Python uses the JavaScript Object Notation module

  • dumps() / loads() → string conversion

  • dump() / load() → file handling

  • Very common in APIs & interviews

  • Must-know topic for Python developers

You may also like...