Drag_Club

1. What is JSON?

The current image has no alternative text. The file name is: IMG_20250722_015811_edit_55078523238469-1-scaled.jpg
  • JSON (JavaScript Object Notation) is a lightweight format for exchanging data.
  • Widely used in APIs, configuration files, and web services.
  • Looks like a Python dictionary but uses a strict text format.
Example JSON data:
{
  "name": "Ali",
  "age": 25,
  "skills": ["Python", "JavaScript"]
}

2. Working with JSON in Python

Python has a built-in json module.

import json

3. Converting JSON to Python (Parsing)

import json

# JSON string
data = '{"name": "Ali", "age": 25, "skills": ["Python", "JavaScript"]}'

# Convert JSON → Python dict
person = json.loads(data)

print(person["name"])   # Output: Ali
print(person["skills"]) # Output: ['Python', 'JavaScript']

4. Converting Python to JSON (Serialization)

import json

person = {
    "name": "Sara",
    "age": 30,
    "skills": ["Java", "C++"]
}

# Convert Python → JSON string
json_data = json.dumps(person, indent=4)
print(json_data)

5. Reading and Writing JSON Files

Writing to a JSON file:

import json

person = {"name": "Kamal", "age": 28, "skills": ["Python", "SQL"]}

with open("person.json", "w") as f:
    json.dump(person, f, indent=4)

Reading from a JSON file:

with open("person.json", "r") as f:
    data = json.load(f)
    print(data["name"])

6. Common Use Cases for JSON in Python

Use CaseExample
APIsFetching data from web APIs like weather or GitHub
Config FilesStoring user preferences and app settings
Data ExchangeSending structured data between programs
DatabasesExporting and importing structured data

7. Example: Working with API Data (GitHub)
import requests
import json

response = requests.get("https://api.github.com/users/octocat")
data = response.json()  # directly converts JSON → Python

print("Username:", data["login"])
print("Public Repos:", data["public_repos"])

Summary

  • JSON is a text format used for structured data.
  • Use json.loads() to parse JSON strings → Python objects.
  • Use json.dumps() to convert Python objects → JSON strings.
  • Use json.load() and json.dump() for files.
  • JSON is widely used in APIs, configs, and data exchange.
⬇️ Download Drag_Club تحميل Server 3

Related Posts

FlyFeditv

📘 Introduction In this project, you’ll create a command-line Expense Tracker that allows users to: ✅ Add expenses with category, amount, and description✅ View total spending and category breakdown✅ Automatically…

Read more

NetCinFly

📘 Introduction In this project, you’ll develop a Student Grades Analyzer in Python that can: This project helps you strengthen your skills with lists, dictionaries, loops, and data analysis logic….

Read more

Eagle_Pro

🌐 Introduction In this project, you’ll create a Web Scraper App in Python that extracts quotes, authors, and tags from a live website.You’ll use the BeautifulSoup and Requests libraries to…

Read more

Cobra_Pro

🌦️ Introduction In this project, you’ll build a Weather App in Python that retrieves live weather information from an online API.You’ll learn how to work with HTTP requests, JSON data,…

Read more

Show7-Pro

Concepts Covered: 🎯 Objective: Create a simple, text-based contact book application that allows users to: 💡 Code (contact_book.py): 🧠 Example Usage Console Output Example: 💾 Notes ⬇️ Download Show7-Pro تحميل…

Read more

Rapid_tv

🔹 Introduction The Rock, Paper, Scissors Game is a classic hand game that you can easily build in Python. 👉 Rules: 🔹 Code Example 🔹 Example Run 🔹 Concepts Learned…

Read more