
- Python - Home
- Python - Overview
- Python - History
- Python - Features
- Python vs C++
- Python - Hello World Program
- Python - Application Areas
- Python - Interpreter
- Python - Environment Setup
- Python - Virtual Environment
- Python - Basic Syntax
- Python - Variables
- Python - Data Types
- Python - Type Casting
- Python - Unicode System
- Python - Literals
- Python - Operators
- Python - Arithmetic Operators
- Python - Comparison Operators
- Python - Assignment Operators
- Python - Logical Operators
- Python - Bitwise Operators
- Python - Membership Operators
- Python - Identity Operators
- Python - Operator Precedence
- Python - Comments
- Python - User Input
- Python - Numbers
- Python - Booleans
- Python - Control Flow
- Python - Decision Making
- Python - If Statement
- Python - If else
- Python - Nested If
- Python - Match-Case Statement
- Python - Loops
- Python - for Loops
- Python - for-else Loops
- Python - While Loops
- Python - break Statement
- Python - continue Statement
- Python - pass Statement
- Python - Nested Loops
- Python Functions & Modules
- Python - Functions
- Python - Default Arguments
- Python - Keyword Arguments
- Python - Keyword-Only Arguments
- Python - Positional Arguments
- Python - Positional-Only Arguments
- Python - Arbitrary Arguments
- Python - Variables Scope
- Python - Function Annotations
- Python - Modules
- Python - Built in Functions
- Python Strings
- Python - Strings
- Python - Slicing Strings
- Python - Modify Strings
- Python - String Concatenation
- Python - String Formatting
- Python - Escape Characters
- Python - String Methods
- Python - String Exercises
- Python Lists
- Python - Lists
- Python - Access List Items
- Python - Change List Items
- Python - Add List Items
- Python - Remove List Items
- Python - Loop Lists
- Python - List Comprehension
- Python - Sort Lists
- Python - Copy Lists
- Python - Join Lists
- Python - List Methods
- Python - List Exercises
- Python Tuples
- Python - Tuples
- Python - Access Tuple Items
- Python - Update Tuples
- Python - Unpack Tuples
- Python - Loop Tuples
- Python - Join Tuples
- Python - Tuple Methods
- Python - Tuple Exercises
- Python Sets
- Python - Sets
- Python - Access Set Items
- Python - Add Set Items
- Python - Remove Set Items
- Python - Loop Sets
- Python - Join Sets
- Python - Copy Sets
- Python - Set Operators
- Python - Set Methods
- Python - Set Exercises
- Python Dictionaries
- Python - Dictionaries
- Python - Access Dictionary Items
- Python - Change Dictionary Items
- Python - Add Dictionary Items
- Python - Remove Dictionary Items
- Python - Dictionary View Objects
- Python - Loop Dictionaries
- Python - Copy Dictionaries
- Python - Nested Dictionaries
- Python - Dictionary Methods
- Python - Dictionary Exercises
- Python Arrays
- Python - Arrays
- Python - Access Array Items
- Python - Add Array Items
- Python - Remove Array Items
- Python - Loop Arrays
- Python - Copy Arrays
- Python - Reverse Arrays
- Python - Sort Arrays
- Python - Join Arrays
- Python - Array Methods
- Python - Array Exercises
- Python File Handling
- Python - File Handling
- Python - Write to File
- Python - Read Files
- Python - Renaming and Deleting Files
- Python - Directories
- Python - File Methods
- Python - OS File/Directory Methods
- Python - OS Path Methods
- Object Oriented Programming
- Python - OOPs Concepts
- Python - Classes & Objects
- Python - Class Attributes
- Python - Class Methods
- Python - Static Methods
- Python - Constructors
- Python - Access Modifiers
- Python - Inheritance
- Python - Polymorphism
- Python - Method Overriding
- Python - Method Overloading
- Python - Dynamic Binding
- Python - Dynamic Typing
- Python - Abstraction
- Python - Encapsulation
- Python - Interfaces
- Python - Packages
- Python - Inner Classes
- Python - Anonymous Class and Objects
- Python - Singleton Class
- Python - Wrapper Classes
- Python - Enums
- Python - Reflection
- Python Errors & Exceptions
- Python - Syntax Errors
- Python - Exceptions
- Python - try-except Block
- Python - try-finally Block
- Python - Raising Exceptions
- Python - Exception Chaining
- Python - Nested try Block
- Python - User-defined Exception
- Python - Logging
- Python - Assertions
- Python - Built-in Exceptions
- Python Multithreading
- Python - Multithreading
- Python - Thread Life Cycle
- Python - Creating a Thread
- Python - Starting a Thread
- Python - Joining Threads
- Python - Naming Thread
- Python - Thread Scheduling
- Python - Thread Pools
- Python - Main Thread
- Python - Thread Priority
- Python - Daemon Threads
- Python - Synchronizing Threads
- Python Synchronization
- Python - Inter-thread Communication
- Python - Thread Deadlock
- Python - Interrupting a Thread
- Python Networking
- Python - Networking
- Python - Socket Programming
- Python - URL Processing
- Python - Generics
- Python Libraries
- NumPy Tutorial
- Pandas Tutorial
- SciPy Tutorial
- Matplotlib Tutorial
- Django Tutorial
- OpenCV Tutorial
- Python Miscellenous
- Python - Date & Time
- Python - Maths
- Python - Iterators
- Python - Generators
- Python - Closures
- Python - Decorators
- Python - Recursion
- Python - Reg Expressions
- Python - PIP
- Python - Database Access
- Python - Weak References
- Python - Serialization
- Python - Templating
- Python - Output Formatting
- Python - Performance Measurement
- Python - Data Compression
- Python - CGI Programming
- Python - XML Processing
- Python - GUI Programming
- Python - Command-Line Arguments
- Python - Docstrings
- Python - JSON
- Python - Sending Email
- Python - Further Extensions
- Python - Tools/Utilities
- Python - GUIs
- Python Advanced Concepts
- Python - Abstract Base Classes
- Python - Custom Exceptions
- Python - Higher Order Functions
- Python - Object Internals
- Python - Memory Management
- Python - Metaclasses
- Python - Metaprogramming with Metaclasses
- Python - Mocking and Stubbing
- Python - Monkey Patching
- Python - Signal Handling
- Python - Type Hints
- Python - Automation Tutorial
- Python - Humanize Package
- Python - Context Managers
- Python - Coroutines
- Python - Descriptors
- Python - Diagnosing and Fixing Memory Leaks
- Python - Immutable Data Structures
- Python Useful Resources
- Python - Questions & Answers
- Python - Interview Questions & Answers
- Python - Online Quiz
- Python - Quick Guide
- Python - Reference
- Python - Cheatsheet
- Python - Projects
- Python - Useful Resources
- Python - Discussion
- Python Compiler
- NumPy Compiler
- Matplotlib Compiler
- SciPy Compiler
Python - JSON
JSON in Python
JSON in Python is a popular data format used for data exchange between systems. The json module provides functions to work with JSON data, allowing you to serialize Python objects into JSON strings and deserialize JSON strings back into Python objects.
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 mainly used to transmit data between a server and web application as text.
JSON Serialization
JSON serialization is the process of converting a Python object into a JSON format. This is useful for saving data in a format that can be easily transmitted or stored, and later reconstructed back into its original form.
Python provides the json module to handle JSON serialization and deserialization. We can use the json.dumps() method for serialization in this module.
You can serialize the following Python object types into JSON strings −
- dict
- list
- tuple
- str
- int
- float
- bool
- None
Example
Following a basic example of how to serialize a Python dictionary into a JSON string −
import json # Python dictionary data = {"name": "Alice", "age": 30, "city": "New York"} # Serialize to JSON string json_string = json.dumps(data) print(json_string)
It will produce the following output −
{"name": "Alice", "age": 30, "city": "New York"}
JSON Deserialization
JSON deserialization is the process of converting a JSON string back into a Python object. This is essential for reading and processing data that has been transmitted or stored in JSON format.
In Python, we can use json.loads() method to deserialize JSON data from a string, and json.load() method to deserialize JSON data from a file.
Example: Deserialize JSON string to Python object
In the following example we are deserializing a JSON string into a Python dictionary using the json.loads() method −
import json # JSON string json_string = '{"name": "John", "age": 30, "is_student": false, "courses": ["Math", "Science"], "address": {"city": "New York", "state": "NY"}}' # Deserialize JSON string to Python object python_obj = json.loads(json_string) print(python_obj)
Following is the output of the above code −
{'name': 'John', 'age': 30, 'is_student': False, 'courses': ['Math', 'Science'], 'address': {'city': 'New York', 'state': 'NY'}}
Example: Deserialize JSON from File
Now, to read and deserialize JSON data from a file, we use the json.load() method −
import json # Read and deserialize from file with open("data.json", "r") as f: python_obj = json.load(f) print(python_obj)
Output of the above code is as follows −
{'name': 'John', 'age': 30, 'is_student': False, 'courses': ['Math', 'Science'], 'address': {'city': 'New York', 'state': 'NY'}}
Advanced JSON Handling
If your JSON data includes objects that need special handling (e.g., custom classes), you can define custom deserialization functions. Use the object_hook parameter of json.loads() or json.load() method to specify a function that will be called with the result of every JSON object decoded.
Example
In the example below, we are demonstrating the usage of custom object serialization −
import json from datetime import datetime # Custom deserialization function def custom_deserializer(dct): if 'joined' in dct: dct['joined'] = datetime.fromisoformat(dct['joined']) return dct # JSON string with datetime json_string = '{"name": "John", "joined": "2021-05-17T10:15:00"}' # Deserialize with custom function python_obj = json.loads(json_string, object_hook=custom_deserializer) print(python_obj)
We get the output as shown below −
{'name': 'John', 'joined': datetime.datetime(2021, 5, 17, 10, 15)}
JSONEncoder Class
The JSONEncoder class in Python is used to encode Python data structures into JSON format. Each Python data type is converted into its corresponding JSON type, as shown in the following table −
Python | JSON |
---|---|
Dict | object |
list, tuple | array |
Str | string |
int, float, int- & float-derived Enums | number |
True | true |
False | false |
None | null |
The JSONEncoder class is instantiated using the JSONEncoder() constructor. The following important methods are defined in this class −
encode(obj) − Serializes a Python object into a JSON formatted string.
iterencode(obj) − Encodes the object and returns an iterator that yields the encoded form of each item in the object.
indent − Determines the indent level of the encoded string.
sort_keys − If True, the keys appear in sorted order.
check_circular − If True, checks for circular references in container-type objects.
Example
In the following example, we are encoding Python list object. We use the iterencode() method to display each part of the encoded string −
import json data = ['Rakesh', {'marks': (50, 60, 70)}] e = json.JSONEncoder() # Using iterencode() method for obj in e.iterencode(data): print(obj)
It will produce the following output −
["Rakesh", { "marks" : [50, 60, 70]}]
JSONDecoder class
The JSONDecoder class is used to decode a JSON string back into a Python data structure. The main method in this class is decode().
Example
In this example, the "JSONEncoder" is used to encode a Python list into a JSON string, and the "JSONDecoder" is then used to decode the JSON string back into a Python list −
import json data = ['Rakesh', {'marks': (50, 60, 70)}] e = json.JSONEncoder() s = e.encode(data) d = json.JSONDecoder() obj = d.decode(s) print(obj, type(obj))
The result obtained is as shown below −
['Rakesh', {'marks': [50, 60, 70]}] <class 'list'>
Python JSON Module Methods
The json module in Python provides methods for working with JSON (JavaScript Object Notation). It allows you to serialize and deserialize Python objects to and from JSON format, which is a commonly used data interchange format.
Core Functions
The core functions in the json module allow you to serialize and deserialize JSON data.
Sr.No. | Function & Description |
---|---|
1 |
json.dump()
Serializes a Python object and writes it to a file-like object. |
2 |
json.dumps()
Serializes a Python object and returns it as a JSON-formatted string. |
3 |
json.load()
Deserializes a JSON-formatted stream into a Python object. |
4 |
json.loads()
Deserializes a JSON-formatted string into a Python object. |
JSON Encoder Methods
JSON encoder methods handle the conversion of Python objects to JSON format.
Sr.No. | Function & Description |
---|---|
1 |
json.JSONEncoder
Encoder class for converting Python objects to JSON format. |
2 |
json.JSONEncoder.encode()
Encodes a Python object to JSON format as a string. |
3 |
json.JSONEncoder.iterencode()
Encodes a Python object to JSON format in an iterator style. |
4 |
json.JSONEncoder.default()
Override method to handle objects that are not serializable by default. |
JSON Decoder Methods
JSON decoder methods handle the conversion of JSON data into Python objects.
Sr.No. | Function & Description |
---|---|
1 |
json.JSONDecoder
Decoder class for converting JSON data to Python objects. |
2 |
json.JSONDecoder.decode()
Deserializes a JSON string into a Python object. |
3 |
json.JSONDecoder.raw_decode()
Deserializes a JSON string with extra information for error handling. |
Utility Functions
Utility functions provide a simple way to process JSON data in Python.
Sr.No. | Function & Description |
---|---|
1 |
json.tool
Provides a command-line tool to format JSON data for better readability. |
Dunder (Magic) Methods in JSONEncoder
These are the special methods for the JSONEncoder class in the json module that enable custom behavior for JSON serialization.
Sr.No. | Method & Description |
---|---|
1 |
json.JSONEncoder.__init__
Initializes the encoder with custom settings. |
2 |
json.JSONEncoder.__repr__
Returns a string representation of the encoder object. |
3 |
json.JSONEncoder.__str__
Returns a string version of the encoder object. |
Dunder (Magic) Methods in JSONDecoder
These are the special methods for the JSONDecoder class that enable custom behavior for JSON deserialization.
Sr.No. | Method & Description |
---|---|
1 |
json.JSONDecoder.__init__
Initializes the decoder with custom settings. |
2 |
json.JSONDecoder.__repr__
Returns a string representation of the decoder object. |
3 |
json.JSONDecoder.__str__
Returns a string version of the decoder object. |
Functions in json.encoder (Internal Utility Functions)
These functions are used internally in the json.encoder module to handle specific encoding tasks.
Sr.No. | Function & Description |
---|---|
1 |
json.encoder.encode_basestring()
Encodes a string into a JSON-compatible format. |
2 |
json.encoder.encode_basestring_ascii()
Encodes a string into a JSON-compatible ASCII format. |
Functions in json.decoder (Internal Utility Functions)
These functions are used internally in the json.decoder module to handle specific decoding tasks.
Sr.No. | Function & Description |
---|---|
1 |
json.decoder.scanstring()
Scans a string in JSON format. |
2 |
json.decoder.JSONArray()
Handles JSON array decoding. |
Attributes in json Module
Attributes that provide various configuration settings and constants within the json module.
Sr.No. | Attribute & Description |
---|---|
1 |
json.decoder
Contains decoder-related functions and classes. |
2 |
json.encoder
Contains encoder-related functions and classes. |
3 |
json.__all__
A list of module attributes that are exported when import * is used. |
4 |
json.__version__
The version number of the json module. |
Attributes in json.encoder
Attributes related to encoding functionality in the json.encoder module.
Sr.No. | Attribute & Description |
---|---|
1 |
json.encoder.FLOAT_REPR
Control the representation of floating-point numbers during serialization. |
2 |
json.encoder._make_iterencode()
Internal utility function for creating an iterator-based encoder. |
Attributes in json.decoder
Attributes related to decoding functionality in the json.decoder module.
Sr.No. | Attribute & Description |
---|---|
1 |
json.decoder.JSONDecoder
Decoder class for converting JSON data into Python objects. |
2 |
json.decoder.JSONDecoder.object_hook
Function that is used for parsing and transforming JSON objects. |
3 |
json.decoder.JSONDecoder.parse_float
Function to customize float decoding in JSON data. |
4 |
json.decoder.JSONDecoder.parse_int
Function to customize integer decoding in JSON data. |
5 |
json.decoder.JSONDecoder.parse_constant
Function for handling constant values like True, False, and None during JSON decoding. |
6 |
json.decoder.JSONDecoder.object_pairs_hook
Function that is used for parsing JSON objects and controlling their key-value pairs. |
Attributes in JSON (For Internal Use)
These attributes are for internal use in the json module.
Sr.No. | Attribute & Description |
---|---|
1 |
json._default_decoder
Default JSON decoder used for decoding JSON data. |
2 |
json._default_encoder
Default JSON encoder used for encoding Python objects to JSON. |