T
Tech Town
Log InSign Up Free
← Back to Blog

June 19, 2025 · admin

Python JSON


πŸ”„ Python and JSON – Easily Work with Data Like a Pro | TechTown.in

In today’s world of APIs, data sharing, and web development, one format rules them all β€” JSON (JavaScript Object Notation).

It’s lightweight, human-readable, and Python makes it super easy to work with JSON using the built-in json module.

In this article, you’ll learn how to parse, convert, read, write, and handle JSON data using Python β€” with examples that just make sense.


🧠 What is JSON?

JSON (JavaScript Object Notation) is a popular format used for storing and exchanging data, especially in APIs and web apps.

It looks like a Python dictionary, but with a slightly different syntax:

{
  "name": "Tanmay",
  "age": 23,
  "city": "Jaipur"
}

πŸ“¦ Importing the JSON Module

import json

βœ… Now you can convert between Python objects and JSON strings.


πŸ” Convert Python to JSON – json.dumps()

import json

data = {"name": "Tanmay", "age": 23, "city": "Jaipur"}
json_string = json.dumps(data)

print(json_string)

🎯 Output:

{"name": "Tanmay", "age": 23, "city": "Jaipur"}

βœ… This is now a JSON-formatted string β€” ready to send via API, save to file, etc.


πŸ”„ Convert JSON to Python – json.loads()

json_data = '{"name": "Tanmay", "age": 23, "city": "Jaipur"}'
parsed = json.loads(json_data)

print(parsed["city"])  # Output: Jaipur

🎯 Converts string β†’ Python dict.


πŸ“ Write JSON to File – json.dump()

data = {"product": "Laptop", "price": 55000}

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

βœ… This saves the Python object to a file in JSON format.


πŸ“– Read JSON from File – json.load()

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

🎯 Reads file β†’ Python dictionary.


🌐 Real-World Use Case – API Response Parsing

Imagine receiving this JSON from a weather API:

{
  "location": "Delhi",
  "temp": 34,
  "condition": "Sunny"
}

You can parse it like this:

response = '{"location": "Delhi", "temp": 34, "condition": "Sunny"}'
weather = json.loads(response)
print(f"Weather in {weather['location']}: {weather['temp']}Β°C, {weather['condition']}")

πŸ§ͺ Bonus: Formatting JSON Output

Pretty Print with indent

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

Sorted Keys

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

🚫 Common Pitfalls

MistakeFix It By…
Mixing single & double quotesAlways use double quotes in JSON
Trying to dump non-serializable objectsConvert to serializable types (e.g., str, int, list, etc.)
Writing without closing fileUse with open(...) to auto-close

πŸ“ Summary – Python JSON Cheatsheet

TaskMethodDirection
Python β†’ JSON stringjson.dumps()Serialize
JSON string β†’ Pythonjson.loads()Deserialize
Write to filejson.dump()Serialize
Read from filejson.load()Deserialize
Pretty printindent, sort_keysFormatting

🏁 Final Thoughts

Python’s json module makes it incredibly easy to send, receive, and store data β€” especially when working with APIs, web apps, or config files.

Whether you’re a beginner or a pro, learning how to use JSON in Python is a must-have skill for modern development.


πŸ“˜ Keep mastering practical Python at TechTown.in