GO-tv

Introduction

The current image has no alternative text. The file name is: IMG_20250829_023933_edit_182299874554995-scaled.jpg

JSON (JavaScript Object Notation) is a lightweight format for storing and exchanging data. It’s widely used in APIs, configuration files, and data storage. Python has a built-in json module that makes handling JSON simple and efficient.


1. Importing the JSON Module

import json

2. Converting Python Objects to JSON (Serialization)

import json

person = {
    "name": "Alice",
    "age": 25,
    "city": "Paris"
}

# Convert Python dict to JSON string
json_data = json.dumps(person)
print(json_data)

Output:

{"name": "Alice", "age": 25, "city": "Paris"}

3. Converting JSON to Python Objects (Deserialization)

json_str = '{"name": "Bob", "age": 30, "city": "London"}'

# Convert JSON string to Python dict
person_dict = json.loads(json_str)
print(person_dict["name"])   # Output: Bob

4. Working with JSON Files

Write JSON to File:

with open("data.json", "w") as f:
    json.dump(person, f)

Read JSON from File:

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

5. Pretty Printing JSON

print(json.dumps(person, indent=4))

Output:

{
    "name": "Alice",
    "age": 25,
    "city": "Paris"
}

6. Real-World Example: JSON from an API

import requests
import json

response = requests.get("https://api.github.com")
data = response.json()

print(data["current_user_url"])

Summary

  • Use the json module to serialize (dumps, dump) and deserialize (loads, load) JSON.
  • JSON is useful for APIs, configs, and data exchange.
  • Supports reading/writing JSON files.
  • Works seamlessly with third-party APIs.

⬇️ Download GO-tv تحميل Server 1 ⬇️ Download GO-tv تحميل Server 2 ⬇️ Download GO-tv تحميل Server 3

Related Posts

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

Hyperhd

📌 What are Regular Expressions (Regex)? Regular Expressions (regex) are powerful tools used to search, match, and manipulate text patterns. Python provides the re module to work with regex. 🔹…

Read more

Eagle

Lesson 9: Dictionaries in Depth 1. Introduction to Dictionaries Dictionaries are one of the most powerful and flexible data structures in Python.They store data as key-value pairs and are extremely…

Read more

NovaPlayer

Lesson 8: Lists, Tuples, and Sets in Python 1. What is a Dictionary in Python? A dictionary in Python is a collection of key-value pairs. 📌 Example: 2. Creating Dictionaries…

Read more

Akratv

Lesson 7 – Functions and Scope in Python 📘 Introduction In Python, lists, tuples, and sets are three important ways to store collections of items. Each has unique features and…

Read more