Skip to content

Python JSON loads() | Function

  • by

Python JSON loads() Function is used to parse a valid JSON string and convert it into a Dictionary. it can read JSON data from text, JSON, or binary files.

json.loads(s)

Python JSON loads()

Simple example code.

import json

# JSON string:
x = """{
"Name": "John king",
"Contact Number": 1234567890,
"Email": "[email protected]",
"Hobbies":["Gaming", "Reading", "Horse Riding"]
}"""

# parse x:
res = json.loads(x)

print(res)
print(type(res))

Output:

Python JSON loads() Function

Please refer to the following conversion table, which is used by the json.load() and json.loads() method for the translations in decoding.

Another example

import json 
    
# JSON string 
employee ='{"id":"101", "name": "John", "department":"Finance"}'
    
# Convert string to Python dict 
employee_dict = json.loads(employee) 
print(employee_dict) 
    
print(employee_dict['name']) 

Read This file contains the following JSON data.

import json

with open("developer.json", "r") as read_file:
    developer = json.load(read_file)

    for key, value in developer.items():
        print(key, ":", value)
JSONPython
objectdict
arraylist
stringstr
number (int)int
number (real)float
trueTrue
falseFalse
nullNone

Do comment if you have any doubts or suggestions on this Python JSON topic.

Note: IDE: PyCharm 2021.3.3 (Community Edition)

Windows 10

Python 3.10.1

All Python Examples are in Python 3, so Maybe its different from python 2 or upgraded versions.

Leave a Reply

Your email address will not be published. Required fields are marked *