← Winnitude! Search  ·  Reader Mode (131 ms)
Links: Stay in Reader Retro Site Converter Retro Mode: OFF Original Page ↗
WWW.TUTORIALSPOINT.COM · 250 LINKS

Python json.JSONDecoder.__init__() Method

Selected Reading

Python json.JSONDecoder.__init__() Method


Previous Quiz Next

The Python json.JSONDecoder.__init__() method initializes a JSONDecoder instance with optional custom parsing behavior. It allows customization of how JSON data is decoded into Python objects.

This method is called internally when creating an instance of JSONDecoder .

Syntax

Following is the syntax of the Python json.JSONDecoder.__init__() method −

JSONDecoder(__init__(object_hook=None, object_pairs_hook=None, parse_float=None, parse_int=None, parse_constant=None, strict=True))

Parameters

This method accepts the following parameters −

  • object_hook (optional): A function to customize how dictionaries are decoded.
  • object_pairs_hook (optional): A function to customize how ordered key-value pairs are decoded.
  • parse_float (optional): A function that processes floating-point numbers.
  • parse_int (optional): A function that processes integer numbers.
  • parse_constant (optional): A function that processes special constants like NaN , Infinity , and -Infinity .
  • strict (optional, default=True): If False , allows non-standard JSON (e.g., trailing commas).

Return Value

This method does not return a value. It initializes a JSONDecoder instance with the specified parameters.

Example: Creating a Default JSONDecoder

In this example, we create a default instance of JSONDecoder and use it to decode a JSON string −

import json

# Create a JSONDecoder instance
decoder = json.JSONDecoder()

# JSON string
json_data = '{"name": "Alice", "age": 25}'

# Decode JSON
result = decoder.decode(json_data)

print("Decoded Data:", result)

Following is the output of the above code −

Decoded Data: {'name': 'Alice', 'age': 25}

Example: Customize Decoding

We can use the object_hook parameter to modify how JSON objects are decoded −

import json

# Custom function to modify JSON object keys
def custom_decoder(obj):
   return {key.upper(): value for key, value in obj.items()}

# Create a JSONDecoder with object_hook
decoder = json.JSONDecoder(object_hook=custom_decoder)

# JSON string
json_data = '{"name": "Alice", "age": 25}'

# Decode JSON with custom function
result = decoder.decode(json_data)

print("Transformed Data:", result)

Following is the output obtained −

Transformed Data: {'NAME': 'Alice', 'AGE': 25}

Example: Parsing Floating-Point and Integer Numbers

We can use parse_float and parse_int parameters to change how numbers are interpreted −

import json

# Custom parsing functions
def parse_int_custom(value):
   return int(value) * 10

def parse_float_custom(value):
   return round(float(value), 2)

# Create a JSONDecoder with custom number parsing
decoder = json.JSONDecoder(parse_int=parse_int_custom, parse_float=parse_float_custom)

# JSON string
json_data = '{"integer": "5", "floating": "12.678"}'

# Decode JSON
result = decoder.decode(json_data)

print("Parsed Numbers:", result)

We get the output as shown below −

Parsed Numbers: {'integer': 50, 'floating': 12.68}

Example: Handling Special Constants (NaN, Infinity)

Using parse_constant , we can control how special constants are processed −

import json

# Custom function for special constants
def parse_constant_custom(value):
   return f"Constant: {value}"

# Create a JSONDecoder with parse_constant
decoder = json.JSONDecoder(parse_constant=parse_constant_custom)

# JSON string
json_data = '{"value": NaN}'

# Decode JSON
result = decoder.decode(json_data)

print("Decoded Special Constants:", result)

The result obtained is as shown below −

Decoded Special Constants: {'value': 'Constant: NaN'}
python_json.htm Previous Quiz Next Advertisements