Open In App

Python – Difference Between json.load() and json.loads()

Last Updated : 26 Nov, 2020
Improve
Improve
Like Article
Like
Save
Share
Report

JSON (JavaScript Object Notation) is a script (executable) file which is made of text in a programming language, is used to store and transfer the data. It is a language-independent format and is very easy to understand since it is self-describing in nature. Python has a built-in package called json. In this article, we are going to see Json.load and json.loads() methods. Both methods are used for reading and writing from the Unicode string with file. 

json.load()

json.load() takes a file object and returns the json object. It is used to read JSON encoded data from a file and convert it into a Python dictionary and deserialize a file itself i.e. it accepts a file object.

Syntax: json.load(fp, cls=None, object_hook=None, parse_float=None, parse_int=None, parse_constant=None, object_pairs_hook=None, **kw)

Parameters:

fp: File pointer to read text.

object_hook: It is an optional parameter that will be called with the result of any object literal decoded.

parse_float: It is an optional parameter that will be called with the string of every JSON float to be decoded. 

parse_int: It is an optional parameter that will be called with the string of every JSON int to be decoded.

object_pairs_hook: It is an optional parameter that will be called with the result of any object literal decoded with an ordered list of pairs.

Example:

First creating the json file:

Python3




import json
  
data = {
    "name": "Satyam kumar",
    "place": "patna",
    "skills": [
        "Raspberry pi",
        "Machine Learning",
        "Web Development"
    ],
    "email": "xyz@gmail.com",
    "projects": [
        "Python Data Mining",
        "Python Data Science"
    ]
}
with open( "data_file.json" , "w" ) as write:
    json.dump( data , write )


Output:

data_file.json

After, creating json file, let’s use json.load():

Python3




with open("data_file.json", "r") as read_content:
    print(json.load(read_content))


Output:

{‘name’: ‘Satyam kumar’, ‘place’: ‘patna’, ‘skills’: [‘Raspberry pi’, ‘Machine Learning’, ‘Web Development’],
’email’: ‘xyz@gmail.com’, ‘projects’: [‘Python Data Mining’, ‘Python Data Science’]}

json.loads()

json.loads() method can be used to parse a valid JSON string and convert it into a Python Dictionary. It is mainly used for deserializing native string, byte, or byte array which consists of JSON data into Python Dictionary.

Syntax: json.loads(s, encoding=None, cls=None, object_hook=None, parse_float=None, parse_int=None, parse_constant=None, object_pairs_hook=None, **kw)

Parameters:

s: Deserialize str (s) instance containing a JSON document to a Python object using this conversion table.

object_hook: It is an optional parameter that will be called with the result of any object literal decoded.

parse_float: It is an optional parameter that will be called with the string of every JSON float to be decoded. 

parse_int: It is an optional parameter that will be called with the string of every JSON int to be decoded.

object_pairs_hook: It is an optional parameter that will be called with the result of any object literal decoded with an ordered list of pairs.

Example:

Python3




import json 
    
# JSON string: 
# Multi-line string 
data = """{ 
    "Name": "Jennifer Smith", 
    "Contact Number": 7867567898, 
    "Email": "jen123@gmail.com", 
    "Hobbies":["Reading", "Sketching", "Horse Riding"] 
    }"""
    
# parse data: 
res = json.loads( data ) 
    
# the result is a Python dictionary: 
print( res )


Output:

{‘Name’: ‘Jennifer Smith’, ‘Contact Number’: 7867567898, ‘Email’: ‘jen123@gmail.com’,
‘Hobbies’: [‘Reading’, ‘Sketching’, ‘Horse Riding’]}



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads