Python reading JSON File Example | Parse Indian States JSON data in Python

In this tutorial on ‘ Python reading JSON File ‘, Learn how to parse JSON (JavaScript Object Notation) data in Python Programming language.

JSON is quite a widely used data-interchange file format. You’ll come across JSON data file especially when using APIs to request data from websites like Google,¬†Yahoo or Uber. So if you happen to build an App using Python or Java (for Android), the app will have to parse JSON file format. Therefore it becomes quite an Important tutorial to know for Python Developers to how to parse JSON data in Python.

To know more about how to import data in Python using Requests library, see here

Python reading JSON File

In this tutorial, we will use JSON data file about Indian states that contains the City names and their Abbreviations.

Python reading JSON File Example | Parse Indian States JSON data in Python

So here is what JSON data looks like! You can download this IndainStates.json file from here.

IndianStates.json
{

"AP":"Andhra Pradesh",

"AR":"Arunachal Pradesh",

"AS":"Assam",

"BR":"Bihar",

"CG":"Chhattisgarh",

"Chandigarh":"Chandigarh",

"DN":"Dadra and Nagar Haveli",

"DD":"Daman and Diu",

"DL":"Delhi",

"GA":"Goa",

"GJ":"Gujarat",

"HR":"Haryana",

"HP":"Himachal Pradesh",

"JK":"Jammu and Kashmir",

"JH":"Jharkhand",

"KA":"Karnataka",

"KL":"Kerala",

"MP":"Madhya Pradesh",

"MH":"Maharashtra",

"MN":"Manipur",

"ML":"Meghalaya",

"MZ":"Mizoram",

"NL":"Nagaland",

"OR":"Orissa",

"PB":"Punjab",

"PY":"Pondicherry",

"RJ":"Rajasthan",

"SK":"Sikkim",

"TN":"Tamil Nadu",

"TR":"Tripura",

"UP":"Uttar Pradesh",

"UK":"Uttarakhand",

"WB":"West Bengal"

}

If you look at the JSON format data closely, It’s a JSON Object which looks quite similar to Python dictionary. And henceforth, when we parse this JSON data in Python, it acquires the data type of dictionary. However, if you use JSON Array, it becomes of the type list in Python.

JSON Array looks like this:

["Delhi", "Mumbai", "Tripura"]

Steps to be followed!

Now to parse a JSON file in Python, we first import JSON library in our Python, then Open the File in Python and load it to a particular variable. Close the file and then print that variable. Additionally, you can check the type of the variable by using following code:

#Print state type
print(type(states))

You can find complete code for the Parsing Indian States JSON Data here:

Python Code
#import JSON Library
import json

#Open JSON File and load it to a variable as dict data type.
json_file = open("C:/Users/risha/Desktop/Extraction/IndianStates.json","r")
states = json.load(json_file)
json_file.close()

#Print state type
print(type(states))

#Printing Code and the name of its respective state
for x in states:
    print (x + ' is the code for ' + states[x])
Output
C:\Python27\python.exe C:/Users/risha/PycharmProjects/readJSON/readinJSON.py

DN is the code for Dadra and Nagar Haveli
DL is the code for Delhi
WB is the code for West Bengal
HR is the code for Haryana
HP is the code for Himachal Pradesh
UP is the code for Uttar Pradesh
JH is the code for Jharkhand
BR is the code for Bihar
JK is the code for Jammu and Kashmir
Chandigarh is the code for Chandigarh
NL is the code for Nagaland
PY is the code for Pondicherry
TR is the code for Tripura
PB is the code for Punjab
DD is the code for Daman and Diu
RJ is the code for Rajasthan
CG is the code for Chhattisgarh
AP is the code for Andhra Pradesh
AS is the code for Assam
AR is the code for Arunachal Pradesh
GA is the code for Goa
GJ is the code for Gujarat
KA is the code for Karnataka
TN is the code for Tamil Nadu
ML is the code for Meghalaya
MN is the code for Manipur
MH is the code for Maharashtra
KL is the code for Kerala
SK is the code for Sikkim
MP is the code for Madhya Pradesh
UK is the code for Uttarakhand
OR is the code for Orissa
MZ is the code for Mizoram

Process finished with exit code 0

If you like doing this project, you can explore another project on Web Scraping in Python using BeautifulSoup Library here

To know more about Python Programming see here