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

Dauo

1. Introduction Web scraping is the process of extracting data from websites. In Python, we commonly use libraries like requests (to fetch web pages) and BeautifulSoup (to parse and extract…

Read more

Macia_Pro

1. What is an API? 2. The requests Library The most common library for working with APIs in Python. Install if needed: 3. Making a GET Request ✅ GET requests…

Read more

BEASh

1. What Is a Module? A module is simply a Python file (.py) that contains functions, classes, or variables you can reuse in other programs. Example: mymodule.py Using it in…

Read more

Phonex_pro

1. Opening a File Python uses the built-in open() function: File Modes Mode Description “r” Read (default) – opens file for reading; error if file doesn’t exist “w” Write –…

Read more

Tigriptv

Introduction Errors are a normal part of programming. In Python, you will encounter two main types of errors: Python provides a powerful way to handle exceptions so your program doesn’t…

Read more

Cleo_patra

1. Introduction File handling is one of the most important skills in Python. Almost every real-world application needs to read from or write to a file. Python makes file handling…

Read more