Python json.decoder.JSONDecoder.parse_int Attribute



The Python json.decoder.JSONDecoder.parse_int attribute is used to specify a custom function for decoding integer numbers when parsing JSON.

By default, Python uses the built-in int type for handling integers in JSON. However, this attribute allows users to replace it with an alternative function, such as float for conversion or str to store numbers as strings.

Syntax

Following is the syntax of using the parse_int attribute −

json.decoder.JSONDecoder(parse_int=function)

Parameter

It is a function that takes a string and returns an integer representation.

Return Value

The parse_int attribute affects how integers are parsed from JSON and returns a user-defined numeric type.

Example: Using parse_int with Default int

In this example, we parse a JSON string containing integer numbers using the default int type −

import json

# JSON string with integer numbers
json_string = '{"age": 30, "year": 2024}'

# Create JSONDecoder instance with default int
decoder = json.decoder.JSONDecoder(parse_int=int)

# Decode JSON
parsed_data = decoder.decode(json_string)

print("Parsed JSON:", parsed_data)
print("Type of 'age':", type(parsed_data["age"]))

Following is the output obtained −

Parsed JSON: {'age': 30, 'year': 2024}
Type of 'age': <class 'int'>

Example: Using parse_int with float

Using float instead of int will store all integers as floating-point numbers −

import json

# JSON string with integer numbers
json_string = '{"age": 30, "year": 2024}'

# Create JSONDecoder instance with float for integer conversion
decoder = json.decoder.JSONDecoder(parse_int=float)

# Decode JSON
parsed_data = decoder.decode(json_string)

print("Parsed JSON:", parsed_data)
print("Type of 'age':", type(parsed_data["age"]))

Following is the output of the above code −

Parsed JSON: {'age': 30.0, 'year': 2024.0}
Type of 'age': <class 'float'>

Example: Convert Integers to Strings

You can also define a custom function to convert integers to strings while decoding −

import json

# Custom function to convert integers to strings
def int_to_string(value):
   return f"{value} (converted)"

# JSON string with integer numbers
json_string = '{"age": 30, "year": 2024}'

# Create JSONDecoder instance with int_to_string function
decoder = json.decoder.JSONDecoder(parse_int=int_to_string)

# Decode JSON
parsed_data = decoder.decode(json_string)

print("Converted JSON:", parsed_data)

We get the output as shown below −

Converted JSON: {'age': '30 (converted)', 'year': '2024 (converted)'}

Example: Custom Integer Processing

A custom function can modify integers, such as doubling their values while decoding −

import json

# Custom function to double integer values
def double_int(value):
   return int(value) * 2

# JSON string with integer numbers
json_string = '{"age": 30, "year": 2024}'

# Create JSONDecoder instance with double_int function
decoder = json.decoder.JSONDecoder(parse_int=double_int)

# Decode JSON
parsed_data = decoder.decode(json_string)

print("Modified JSON:", parsed_data)

The result produced is as shown below −

Modified JSON: {'age': 60, 'year': 4048}
python_json.htm
Advertisements