codingstuff.io
ExploreTutorialsProblemsCS Subjects
Get Started
ExploreTutorialsProblemsCS Subjects
Get Started
codingstuff.io

Master the art of building software through interactive tutorials, real-world problems, and guided projects.

Pune, Maharashtra, India

codingstuffmail@gmail.com

Product

  • Explore
  • Tutorials
  • Problems
  • CS Subjects

Company

  • About
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Sitemap

© 2026 codingstuff.io. All rights reserved.

Built with ❤️ for developers everywhere

/
/
All Tutorials
🐍

Python Programming

44 / 68 topics
36List Comprehensions37Python Iterators38Python Generators39Python Decorators40Python Modules, Packages & PIP41Python Main Function (__name__ == '__main__')42Python Dates and Time43Python Regular Expressions44Python JSON
Tutorials/Python Programming/Python JSON
🐍Python Programming

Python JSON

Updated 2026-04-20
3 min read

Introduction

JSON (JavaScript Object Notation) is a lightweight data-interchange format that is easy for humans to read and write, and easy for machines to parse and generate. It is based on a subset of the JavaScript Programming Language, Standard ECMA-262 3rd Edition - December 1999. JSON is language-independent but uses conventions familiar to programmers of the C-family of languages, including C, C++, C#, Java, JavaScript, Perl, Python, and many others.

In this tutorial, we will explore how to work with JSON in Python using the built-in json module. We'll cover reading from and writing to JSON files, handling complex data types, and best practices for working with JSON in Python applications.

Understanding JSON

JSON is a text format that is completely language independent but uses conventions familiar to programmers of the C-family of languages. The JSON format is derived from JavaScript Programming Language, Standard ECMA-262 3rd Edition - December 1999. These are the two primary structures in JSON:

  1. Object: An unordered set of name/value pairs.

    {
      "name": "John",
      "age": 30,
      "city": "New York"
    }
    
  2. Array: An ordered collection of values.

    [
      {
        "name": "Alice",
        "age": 25
      },
      {
        "name": "Bob",
        "age": 30
      }
    ]
    

Working with JSON in Python

Python provides a built-in json module that allows you to encode and decode JSON data. Let's explore how to use this module.

Importing the json Module

First, you need to import the json module:

import json

Encoding (Serializing) Python Objects to JSON

To convert a Python object into a JSON string, you can use the json.dumps() method. This method takes a Python object and returns a JSON string.

Example: Serializing a Dictionary

# Define a dictionary
data = {
    "name": "John",
    "age": 30,
    "city": "New York"
}

# Serialize the dictionary to a JSON string
json_string = json.dumps(data)
print(json_string)  # Output: {"name": "John", "age": 30, "city": "New York"}

Example: Serializing with Formatting

You can also format the output for better readability by using the indent parameter:

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

Output:

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

Decoding (Deserializing) JSON to Python Objects

To convert a JSON string back into a Python object, you can use the json.loads() method. This method takes a JSON string and returns a Python object.

Example: Deserializing a JSON String

# Define a JSON string
json_string = '{"name": "John", "age": 30, "city": "New York"}'

# Deserialize the JSON string to a Python dictionary
data = json.loads(json_string)
print(data)  # Output: {'name': 'John', 'age': 30, 'city': 'New York'}

Working with JSON Files

For reading from and writing to JSON files, you can use json.dump() and json.load() methods.

Example: Writing a Python Object to a JSON File

# Define a dictionary
data = {
    "name": "John",
    "age": 30,
    "city": "New York"
}

# Write the dictionary to a JSON file
with open('data.json', 'w') as json_file:
    json.dump(data, json_file, indent=4)

Example: Reading from a JSON File

# Read the JSON file and load its content into a Python object
with open('data.json', 'r') as json_file:
    data = json.load(json_file)

print(data)  # Output: {'name': 'John', 'age': 30, 'city': 'New York'}

Handling Complex Data Types

The json module can handle basic data types like strings, numbers, lists, and dictionaries. However, it cannot serialize complex Python objects like datetime or custom classes by default.

Custom Serialization with default Parameter

You can provide a default function to handle serialization of unsupported types:

from datetime import datetime

def datetime_handler(x):
    if isinstance(x, datetime):
        return x.isoformat()
    raise TypeError("Unknown type")

data = {
    "name": "John",
    "timestamp": datetime.now()
}

json_string = json.dumps(data, default=datetime_handler)
print(json_string)

Using JSONEncoder Subclass

You can also subclass json.JSONEncoder to customize serialization:

import json
from datetime import datetime

class DateTimeEncoder(json.JSONEncoder):
    def default(self, obj):
        if isinstance(obj, datetime):
            return obj.isoformat()
        return super().default(obj)

data = {
    "name": "John",
    "timestamp": datetime.now()
}

json_string = json.dumps(data, cls=DateTimeEncoder)
print(json_string)

Best Practices

  1. Error Handling: Always use try-except blocks when working with JSON to handle potential errors like malformed JSON data.

  2. Security: Be cautious when parsing JSON data from untrusted sources to avoid security vulnerabilities like injection attacks.

  3. Performance: For large datasets, consider using libraries like ujson or orjson for faster JSON processing.

  4. Consistency: Use consistent formatting and naming conventions in your JSON data for better readability and maintainability.

  5. Documentation: Document the structure of your JSON data to help other developers understand how to use it.

Conclusion

In this tutorial, we have covered the basics of working with JSON in Python using the json module. We explored encoding and decoding JSON data, handling complex data types, and best practices for working with JSON in Python applications. By following these guidelines, you can effectively manage JSON data in your Python projects.

Remember to always test your JSON operations thoroughly and handle potential errors gracefully to ensure robustness in your applications.


PreviousPython Regular ExpressionsNext Python File Handling (Read/Write/Delete)

Recommended Gear

Python Regular ExpressionsPython File Handling (Read/Write/Delete)