Header Ads Widget

⚡ Premium Tools Hub • EXE Apps + Full Python Source Code
Lite • Pro • Bundle Packs • Instant Download

Python JSON Tutorial – Parse, Read, Write JSON Data Easily

Python JSON

JSON (JavaScript Object Notation) is one of the most popular data formats used for storing and exchanging data.

It is widely used in:

  • Web APIs
  • Mobile applications
  • Configuration files
  • Cloud services
  • Databases

Python provides a built-in module called json to work with JSON data easily.


What is JSON?

JSON is a lightweight data format used to represent structured data.

Example JSON:

{
    "name": "John",
    "age": 30,
    "city": "New York"
}

Why Use JSON?

  • Easy to read and write
  • Lightweight format
  • Language independent
  • Perfect for APIs
  • Fast data exchange

Python JSON Module

Python provides the built-in json module.

import json

1. Converting Python to JSON (Serialization)

Use json.dumps().

import json

data = {
    "name": "John",
    "age": 30,
    "city": "New York"
}

json_string = json.dumps(data)

print(json_string)

Output

{"name": "John", "age": 30, "city": "New York"}

2. Converting JSON to Python (Deserialization)

Use json.loads().

import json

json_data = '{"name": "John", "age": 30}'

python_obj = json.loads(json_data)

print(python_obj)

Output

{'name': 'John', 'age': 30}

3. Reading JSON from File

import json

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

print(data)

4. Writing JSON to File

import json

data = {
    "name": "Alice",
    "age": 25
}

with open("output.json", "w") as file:
    json.dump(data, file)

5. Pretty Printing JSON

import json

data = {"name": "John", "age": 30}

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

Output

{
    "name": "John",
    "age": 30
}

6. Sorting JSON Keys

print(json.dumps(data, sort_keys=True))

7. JSON Data Types Mapping

JSONPython
objectdict
arraylist
stringstr
numberint / float
trueTrue
falseFalse
nullNone

8. Handling Complex JSON

data = {
    "users": [
        {"name": "John", "age": 30},
        {"name": "Alice", "age": 25}
    ]
}

print(json.dumps(data, indent=2))

9. Error Handling in JSON

JSON Decode Error

json.decoder.JSONDecodeError

Solution:

  • Check JSON format
  • Ensure proper quotes

10. Working with APIs

Example API response:

import json

response = '{"status": "success", "data": [1, 2, 3]}'

data = json.loads(response)

print(data["status"])

11. JSON vs XML

Feature    JSON    XML
Readability    High    Medium
Size    Smaller    Larger
Speed    Faster    Slower
Usage    APIs    Legacy systems

Real-World Applications

JSON is used in:

  • REST APIs
  • Web applications
  • Mobile apps
  • Configuration files
  • Cloud services
  • Microservices communication

Best Practices

  • Use indent for readability
  • Validate JSON before parsing
  • Handle exceptions properly
  • Use JSON for API communication
  • Avoid manual string formatting

Common Mistakes

Using single quotes in JSON

{'name': 'John'}

Correct:

{"name": "John"}

Forgetting json.loads()

Always convert JSON string before using.


Summary

Python provides a powerful built-in json module for handling structured data. It allows developers to easily convert between Python objects and JSON format for storage, APIs, and data exchange.


Conclusion

JSON is essential for modern web development and APIs. Python makes JSON processing simple, fast, and efficient. Mastering JSON will help you build better web applications, APIs, and data-driven systems.




Post a Comment

0 Comments