Read JSON File in Python
JSON refers to the JavaScript Object Notation. It is a Data format used for representing structured data and it is used to transfer and store data. In JSON format, the receiving and transmitting of data are simple between a web application and a server.
JSON is a built-in package in Python. we can import the json package in Python script to use the feature. The value in key-value mapping inside { } is contained by the quoted string. It helps to do the text in JSON.
About JSON
JSON(JavaScript Object Notation) refers to the JavaScript Object Notation. It is a Data format(open standard file format) and it is also called data exchange format that is used for representing structured data and is used to transmit and store data. JSON protocols are well known to programmers which include C++, C, Python, Java, Perl, etc.
- JSON stands for JavaScript Object Notation.
- The format was defined by Douglas Crockford.
- It was developed for human-readable data interchange.
- It has been derived from the JavaScript scripting language.
- It is language-independent.
- The filename is saved as .json.
- The Internet Media Type of JSON is application/json.
- The Identifier of Uniform Type is public.json.
Usage of JSON:
- It is used while writing JavaScript-based applications that need browser extensions and websites.
- JSON format can be used for transmitting and serializing structured data on a network connection.
- It is originally used to transfer data among web applications and a server.
- It is highly used by APIs and web services for providing public data.
- It can be used with standard programming languages.
Characteristics of JSON:
- This is easy to write and read.
- It is a lightweight text-based exchange format.
- It is language-independent.
Import JSON Module
We have to import the json package to work with JSON.
import json
Reading From JSON
Loadin a JSON object in Python is very easy. The built-in package named json in Python and it is used to work with JSON Files. json Module provides various methods such as load() and loads() methods are used in reading the JSON file.
Deserializing JSON
The load() and loads() methods in json library are used for converting the JSON objects into their identical Python objects.
the load() and loads() are easily deserialized the json data or string format of JSON. Normally load() and loads() load from string, unless the source object is in dict or list.
For example:
JSON Python
Object dict
array list
string str
number (int) int
number (real) float
true True
false False
null None
Built-in Methods to Read JSON File
Below are the built-in json method which is used to read the JSON file in Python.
json.load() method
json.load() reads the JSON object file and converts the JSON data, then the Python dictionary is populated with the data and returns the identical python objects.
Syntax:
json.load(file object)
Example:
Consider a file having name Jhon.json and it contains JSOn object.
{"name": "Jhon",
"languages": ["English", "French"]
}
import json
with open('path_to_file/Jhon.json') as f:
data = json.load(f)
print(data)
Output:
{'name': 'Jhon', 'languages': ['English', 'French']}
json.loads() method
In json.loads() method, a JSON string can be parsed. it takes the file contents as a string, no matter that what's the file path is.
the fileobject.read() is used with json.loads() to return the file content.
Syntax:
json.loads(jsonstring) #for Json string
json.loads(fileobject.read()) #for fileobject
Example:
import json
person_data = '{ "person": { "name": "Jhon", "sex": "male", "age": 32}}'
# converting JSON format in dictionary
dict_obj = json.loads(person_data)
print(dict_obj)
# check type of dict_obj
print("Type of dict_obj", type(dict_obj))
# get object details
print("Person......", dict_obj.get('person'))
Output:
{'person': {'name': 'Jhon', 'sex': 'male', 'age': 32}}
Type of dict_obj <class 'dict'>
Person...... {'name': 'Jhon', 'sex': 'male', 'age': 32}
Converting Python to JSON objects:
We can also convert Python dictionary objects into JSON objects with json.dumps() method.
Two parameters are used in json.dumps:
- Dictionary: dictionary name which has to convert into JSON object.
- indent: unit numbers are defined for indentation.
Syntax:
json.dumps(dict, indent)
Example:
import json
# Data
dictionary ={
"id": "06",
"name": "JHON",
"depatment": "IT"
}
# Serializing
json_object = json.dumps(dictionary, indent = 4)
print(json_object)
Output:
{
"depatment": "IT",
"id": "06",
"name": "JHON"
}
Conclusion
In this tutorial, we have discussed the Pythom JSON read File. we have also learned about the JSON framework and its characteristics with uses. Now we understood how to covert the JSON objects into Python dictionary objects and vice versa.