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
Difference Between Node.js and Python
Node.js and Python are two of the most popular programming languages for backend development. Each has its own strengths and weaknesses, and the choice between them often depends on the specific requirements of the project. This article provides a detailed comparison of Node.js and Python, highlight
4 min read
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 for loop and while loop in Python
In this article, we will learn about the difference between for loop and a while loop in Python. In Python, there are two types of loops available which are 'for loop' and 'while loop'. The loop is a set of statements that are used to execute a set of statements more than one time. For example, if w
4 min read
Difference between Python and JavaScript
Python and JavaScript are both popular programming languages, each with distinct features. Python emphasizes readability and simplicity, ideal for tasks like data analysis and backend development, while JavaScript is primarily used for web development, offering dynamic and interactive functionality
4 min read
Python: Difference between dir() and help()
In Python, the dir() and help() functions help programmers understand objects and their functionality.dir() lists all the attributes and methods available for an object, making it easy to explore what it can do.help() provides detailed information about an object, including descriptions of its metho
4 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
Difference Between List and Tuple in Python
In Python, lists and tuples both store collections of data, but differ in mutability, performance and memory usage. Lists are mutable, allowing modifications, while tuples are immutable. Choosing between them depends on whether you need to modify the data or prioritize performance and memory efficie
4 min read
Difference between Python and C++
Python and C++ both are the most popular and general-purpose programming languages. They both support Object-Oriented Programming (OPP) yet they are a lot different from one another. In this article, we will discuss how Python is different from C++. What is Python?Python is a high-level, interpreted
4 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 dir() and vars() in Python
As Python stores their instance variables in the form of dictionary belonging to the respective object both dir() and vars() functions are used to list the attributes of an instance/object of the Python class. Though having a similar utility these functions have significant individual use cases.dir(
2 min read