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:
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’]}
Python – Difference Between json.load() and json.loads()
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.