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
import json
data = {
"name": "Satyam kumar",
"place": "patna",
"skills": [
"Raspberry pi",
"Machine Learning",
"Web Development"
],
"email": "[email protected]",
"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': '[email protected]', '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": "[email protected]",
"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': '[email protected]',
'Hobbies': ['Reading', 'Sketching', 'Horse Riding']}
Similar Reads
Python - Difference between json.dump() and json.dumps() JSON is a lightweight data format for data interchange which can be easily read and written by humans, easily parsed and generated by machines. It is a complete language-independent text format. To work with JSON data, Python has a built-in package called json. Note: For more information, refer to
2 min read
Difference between List and Array in Python In Python, lists and arrays are the data structures that are used to store multiple items. They both support the indexing of elements to access them, slicing, and iterating over the elements. In this article, we will see the difference between the two.Operations Difference in Lists and ArraysAccessi
6 min read
json.loads() vs json.loads() in Python orjson.loads() and json.loads() are both Python methods used to deserialize (convert from a string representation to a Python object) JSON data. orjson and json are both Python libraries that provide functions for encoding and decoding JSON data. However, they have some differences in terms of perfo
4 min read
Difference between List and Dictionary in Python Lists and Dictionaries in Python are inbuilt data structures that are used to store data. Lists are linear in nature whereas dictionaries stored the data in key-value pairs. In this article, we will see the difference between the two and find out the time complexities and space complexities which ar
6 min read
What Is The Difference Between .Py And .Pyc Files? When working with Python, you might have come across different file extensions like .py and .pyc. Understanding the differences between these two types of files is essential for efficient Python programming and debugging. '.py' files contain human-readable source code written by developers, while '.
3 min read
What is the difference between Python's Module, Package and Library? In this article, we will see the difference between Python's Module, Package, and Library. We will also see some examples of each to things more clear. What is Module in Python? The module is a simple Python file that contains collections of functions and global variables and with having a .py exten
2 min read
Difference between various Implementations of Python When we speak of Python we often mean not just the language but also the implementation. Python is actually a specification for a language that can be implemented in many different ways. Background Before proceeding further let us understand the difference between bytecode and machine code(native co
4 min read
Difference between Numpy array and Numpy matrix While working with Python many times we come across the question that what exactly is the difference between a numpy array and numpy matrix, in this article we are going to read about the same. What is np.array() in PythonThe Numpy array object in Numpy is called ndarray. We can create ndarray using
3 min read
json.loads() in Python JSON is a lightweight data format used for storing and exchanging data across systems. Python provides a built-in module called json to work with JSON data easily. The json.loads() method of JSON module is used to parse a valid JSON string and convert it into a Python dictionary. For example:Pythoni
4 min read