Related Articles

Related Articles

Python – Difference Between json.load() and json.loads()
  • Last Updated : 26 Nov, 2020

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

filter_none

edit
close

play_arrow

link
brightness_4
code

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 )

chevron_right


Output:



data_file.json

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

Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


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

filter_none

edit
close

play_arrow

link
brightness_4
code

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 )

chevron_right


Output:

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

Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.

My Personal Notes arrow_drop_up
Recommended Articles
Page :