Movatterモバイル変換


[0]ホーム

URL:


Python Tutorial

Python json.decoder.JSONDecoder.parse_int Attribute



The Pythonjson.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-inint type for handling integers in JSON. However, this attribute allows users to replace it with an alternative function, such asfloat for conversion orstr to store numbers as strings.

Syntax

Following is the syntax of using theparse_int attribute −

json.decoder.JSONDecoder(parse_int=function)

Parameter

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

Return Value

Theparse_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 defaultint type −

import json# JSON string with integer numbersjson_string = '{"age": 30, "year": 2024}'# Create JSONDecoder instance with default intdecoder = json.decoder.JSONDecoder(parse_int=int)# Decode JSONparsed_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

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

import json# JSON string with integer numbersjson_string = '{"age": 30, "year": 2024}'# Create JSONDecoder instance with float for integer conversiondecoder = json.decoder.JSONDecoder(parse_int=float)# Decode JSONparsed_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 stringsdef int_to_string(value):   return f"{value} (converted)"# JSON string with integer numbersjson_string = '{"age": 30, "year": 2024}'# Create JSONDecoder instance with int_to_string functiondecoder = json.decoder.JSONDecoder(parse_int=int_to_string)# Decode JSONparsed_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 valuesdef double_int(value):   return int(value) * 2# JSON string with integer numbersjson_string = '{"age": 30, "year": 2024}'# Create JSONDecoder instance with double_int functiondecoder = json.decoder.JSONDecoder(parse_int=double_int)# Decode JSONparsed_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
Print Page
Advertisements

[8]ページ先頭

©2009-2025 Movatter.jp