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’]}
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.