The Python json.loads() function is used to parse a JSON-formatted string and convert it into a corresponding Python object.
This function is useful when working with JSON data received from APIs, reading configuration settings, or processing structured data stored as a string.
SyntaxFollowing is the syntax of the Python json.loads() function −
json.loads(s, *, cls=None, object_hook=None, parse_float=None, parse_int=None, parse_constant=None, object_pairs_hook=None)Parameters
This function accepts the following parameters −
This function returns a Python object representing the parsed JSON data.
Example: Basic UsageIn this example, we use the json.loads() function to convert a JSON-formatted string into a Python dictionary −
import json # JSON string json_string = '{"name": "John", "age": 30, "city": "New York"}' # Convert JSON string to Python dictionary data = json.loads(json_string) print("Parsed Data:", data)
We get the output as shown below −
Parsed Data: {'name': 'John', 'age': 30, 'city': 'New York'}Example: Custom Parsing
The object_hook parameter allows us to define a custom function that modifies the JSON objects when they are loaded. In this example, we convert all keys to uppercase −
import json # Custom function to transform JSON objects def custom_decoder(obj): return {key.upper(): value for key, value in obj.items()} # JSON string json_string = '{"name": "John", "age": 30, "city": "New York"}' # Convert JSON string to dictionary with custom object_hook data = json.loads(json_string, object_hook=custom_decoder) print("Transformed Data:", data)
Following is the transformed output −
Transformed Data: {'NAME': 'John', 'AGE': 30, 'CITY': 'New York'}Example: Parsing Numbers
We can use the parse_int and parse_float parameters to customize how numbers are processed while loading JSON data −
import json # Custom parsing functions def parse_int_custom(value): return int(value) * 10 # Multiply integers by 10 def parse_float_custom(value): return round(float(value), 2) # Round floats to 2 decimal places # JSON string json_string = '{"integer": "10", "floating": "12.3456"}' # Convert JSON string to dictionary with custom number parsing data = json.loads(json_string, parse_int=parse_int_custom, parse_float=parse_float_custom) print("Parsed Data:", data)
Following is the output obtained −
Parsed Data: {'integer': 100, 'floating': 12.35}Example: Preserving Key Order
By default, Python dictionaries do not maintain order before Python 3.7. If you need to preserve the order of keys while loading JSON, you can use the object_pairs_hook parameter with OrderedDict −
import json from collections import OrderedDict # JSON string json_string = '{"name": "John", "age": 30, "city": "New York"}' # Convert JSON string to OrderedDict data = json.loads(json_string, object_pairs_hook=OrderedDict) print("Ordered Data:", data)
The result produced is as follows −
Ordered Data: OrderedDict({'name': 'John', 'age': 30, 'city': 'New York'})Example: Handling Special Constants
The parse_constant parameter allows us to customize how special constants like NaN and Infinity are handled −
import json # Custom function for parsing special constants def custom_parse_constant(value): return f"Constant: {value}" # JSON string with special constants json_string = '{"number": Infinity, "value": NaN}' # Convert JSON string to dictionary with custom parse_constant data = json.loads(json_string, parse_constant=custom_parse_constant) print("Parsed Data:", data)
Following is the output obtained −
Parsed Data: {'number': 'Constant: Infinity', 'value': 'Constant: NaN'}
python_json.htm
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4