Python json.JSONDecoder.__str__ Method



The Python json.JSONDecoder.__str__ method returns a user-friendly string representation of the JSONDecoder instance.

This method is useful for displaying information about the decoder instance in a readable format.

Syntax

Following is the syntax of the Python json.JSONDecoder.__str__ method −

JSONDecoder.__str__()

Parameters

This method does not accept any parameters.

Return Value

This method returns a human-readable string representation of the JSONDecoder instance.

Example: Basic Usage

In this example, we use the json.JSONDecoder.__str__() method to get a string representation of a JSONDecoder instance −

Open Compiler
import json # Create JSONDecoder instance decoder = json.JSONDecoder() # Get string representation str_representation = decoder.__str__() print("JSONDecoder String Representation:", str_representation)

Following is the output obtained −

JSONDecoder String Representation: <json.decoder.JSONDecoder object at 0x7f1993db9580>

Example: Using in Print Statements

The __str__ method allows JSONDecoder instances to be directly printed in a readable format −

Open Compiler
import json # Create JSONDecoder instance decoder = json.JSONDecoder() # Print the decoder instance print("Decoder Instance:", decoder)

Following is the output of the above code −

Decoder Instance: <json.decoder.JSONDecoder object at 0x7f7515fa54f0>

Example: Logging JSONDecoder.__str__() Output

The __str__ method can be useful for logging information about the decoder instance −

Open Compiler
import json import logging # Configure logging logging.basicConfig(level=logging.INFO) # Create JSONDecoder instance decoder = json.JSONDecoder() # Log the string representation logging.info("Decoder Instance: %s", str(decoder))

Wwe get the output as shown below −

INFO:root:Decoder Instance: <json.decoder.JSONDecoder object at 0x7f7bf4056060>

Example: Storing JSONDecoder.__str__() Output

We can store the string representation of a JSONDecoder instance for later use −

Open Compiler
import json # Create JSONDecoder instance decoder = json.JSONDecoder() # Store string representation in a variable decoder_info = str(decoder) print("Stored Decoder Info:", decoder_info)

The result produced is as follows −

Stored Decoder Info: <json.decoder.JSONDecoder object at 0x7f26c51520f0>

Example: Comparing JSONDecoder Instances

Using __str__, we can compare different instances of JSONDecoder in string format −

Open Compiler
import json # Create two JSONDecoder instances decoder1 = json.JSONDecoder() decoder2 = json.JSONDecoder() # Compare their string representations print("Decoder 1:", str(decoder1)) print("Decoder 2:", str(decoder2)) print("Are they the same?:", str(decoder1) == str(decoder2))

After executing the above code, we get the following output −

Decoder 1: <json.decoder.JSONDecoder object at 0x7fde4fc2aa50>
Decoder 2: <json.decoder.JSONDecoder object at 0x7fde4fc2ad80>
Are they the same?: False
python_json.htm
Advertisements