Nearby lessons

134 of 159

Python - JSON Guide

📌 What You Will Learn
  • Understand what JSON is and why it is used for data exchange
  • Import the built-in json module
  • Convert Python objects to JSON with json.dumps()
  • Parse JSON into Python objects with json.loads()
  • Read and write JSON files with json.load() and json.dump()

What Is JSON?

JSON stands for JavaScript Object Notation and is a lightweight format for data exchange.

Why JSON Is Required

  • APIs
  • Web applications
  • Configuration files
  • System-to-system data exchange

json Module

Python provides the built-in json module.

import json

Converting Python to JSON

import json

student = {'name': 'Rahul', 'age': 25}
print(json.dumps(student))

Converting JSON to Python

import json

data = '{"name":"Rahul","age":25}'
print(json.loads(data))

JSON Files

Use json.load() to read from a file and json.dump() to write to a file.

📝 Key Takeaways
  • JSON is a lightweight data-interchange format used by APIs, web applications, and configuration files
  • Python provides the built-in json module for working with JSON data
  • json.dumps() serializes Python objects to a JSON string
  • json.loads() parses a JSON string into Python objects
  • Use json.load() to read JSON from a file and json.dump() to write JSON to a file

🧠 Test Your Knowledge

3 Questions
Progress: 0 / 3