Data Science and Machine Learning Internship ...
- 1k Enrolled Learners
- Weekend/Weekday
- Live Class
Do you know how to transport your data from online API’s or store different kinds of data to your local machines? One way or the other you have immersed yourself in JSON which stands for Java Script Object Notation. It is a renowned and popular data format used for representing semi-structured data. Let’s get to know more about Python JSON in detail.
The following aspects will be discussed in this article:
JSON stands for Java Script Object Notation is a way of storing information in an organized and easy manner. The data must be in the form of a text when exchanged between a browser and a server.
In case you are wondering if it is JavaScript? then, the answer is no. It is a script that is made up of text and is used for storing and transferring data in a human and machine-readable format. It is a small, light-weight data format inspired by JavaScript and generally used in text or string format. A packet of JSON is almost identical to a python dictionary. Now, you must be wondering;
The answer to your question is, you have to import the JSON module that generally converts the Python data types into the JSON string file. It consists of JSON functions that read and write directly from JSON files. Python has a built-in JSON package and is a part of the standard library, so you need not install it.
import json
Now that you are aware of JSON in Python, let’s take a deeper look at Parsing.
The JSON library can parse JSON from strings or files. It can also parse JSON into the Python dictionary or list and do the vice-versa. Parsing generally happens in two stages:
Let’s get a better understanding of both stages.
You can convert JSON string to Python by using json.loads().
Let me show you the practical implementation:
import json people_string = ''' { "people":[ { "emp_name": "John smith", "emp_no.": "924367-567-23", "emp_email": ["[email protected]"], "has_license": "false" }, { "emp_name": "harshit kant", "emp_number": "560-555-5153", "emp_email": "null", "has_license": "true" } ] } ''' data = json.loads(people_string) print(data)
As you can see from the above output, it has printed a Python dictionary. Let’s print the datatype for better understanding.
import json people_string = ''' { "people":[ { "emp_name": "John smith", "emp_no.": "924367-567-23", "emp_email": ["[email protected]"], "has_license": "false" }, { "emp_name": "harshit kant", "emp_number": "560-555-5153", "emp_email": "null", "has_license": "true" } ] } ''' data = json.loads(people_string) print(type(data)) #prints the datatype
<class ‘dict’>
Now, that you are familiar with one conversion, let’s see the other conversion type in the second stage.
A Python object can be converted to JSON string by using json.dumps().
Let’s take a look at an example given below:
import json people_string = ''' { "people":[ { "emp_name": "John smith", "emp_no.": "924367-567-23", "emp_email": ["[email protected]"], "has_license": "false" }, { "emp_name": "harshit kant", "emp_no.": "560-555-5153", "emp_email": "null", "has_license": "true" } ] } ''' data = json.loads(people_string) new_string = json.dumps(data) print(new_string)
The output will be of a JSON string type. I have already demonstrated the datatype in JSON to Python conversion, the same procedure is followed will be followed for printing the data type.
Let’s move ahead and see how Pandas parse JSON.
JSON string can be parsed into a pandas Dataframe from the following steps:
import pandas as pd pd.read_json(r'Path where you saved the JSON fileFile Name.json')
The below-implemented code loads my JSON file into the DataFrame.
import pandas as pd import json with open(r'C:UsersHarshit_KantDesktopnobel.prize.json') as f: data = json.load(f) print (data) df = pd.DataFrame print(df)
Moving ahead, let us see how you can serialize JSON in Python.
Serializing JSON simply means that you are encoding JSON. It converts the given Python data structure(ex:dict) into its valid JSON object. To handle the data flow in a file, the JSON library in Python uses a dump() and dumps() method, that does the conversion and makes it easy to write data into files.
Given below is a table illustrating the Python datatypes getting converted to their respective JSON type.
Python | JSON |
dict(dictionary) | object |
list, array | tuple |
string | string |
int, long, float | numbers |
True | true |
False | false |
None | null |
Points to remember:
dump() – Converts the data to a JSON file
dumps() – Converts the data to a JSON string
load() – Converts the JSON file into a Python object
loads() – Converts an object of JSON string into a Python object
Pretty Printing takes care of the code alignment and makes it in a human-readable format. Let’s look at the below example where I have passed two parameters ‘sort_keys’ that always returns a boolean True value and ‘indent’ spaces.
import json people_string = ''' { "people":[ { "emp_name": "John smith", "emp_no.": "924367-567-23", "emp_email": ["[email protected]"], "has_license": "false" }, { "emp_name": "harshit kant", "emp_no.": "560-555-5153", "emp_email": "null", "has_license": "true" } ] } ''' data = json.loads(people_string) new_string = json.dumps(data, sort_keys=True, indent=3) print(new_string)
Output:
Moving ahead in Python JSON tutorial, let us understand the deserialization of JSON.
Deserialization of JSON is the exact opposite of serialization i.e. it means you are decoding JSON. It converts the given JSON string into a Python object by making use of load() and loads() method which does the conversion.
Given below is a table which illustrates the conversion of JSON data type to its respective Python type.
JSON | Python |
object | dict(dictionary) |
tuple | list, array |
string | string |
numbers | int, long, float |
true | True |
false | False |
null | None |
Moving ahead in the “Python JSON” tutorial. I’ll show you a real-time example of both serialization and deserialization through coding perspective.
In this coding demonstration, I am making use of a JSON dataset called “Nobel prize” which is given here. You will learn how to do serialization and deserialization of the same through a JSON file.
import json with open('nobel_prize.json.html') as f: data = json.load(f) with open('new_nobel_prize.json.html') as f: json.dump(data,f,indent=2)
Python code is compiled successfully and a new file “new_nobel_prize.json” is created where the data is being dumped from an already existing file “nobel_prize.json”.
import json with open('nobel_prize.json.html') as f: data = json.load(f) for nobel_prize in data['prizes']: print(nobel_prize['year'],nobel_prize['category'])
The code snippet shows the changes from a JSON file to its respective Python object.
This brings us to the end of our article “Python JSON”. I hope you are clear with all the concepts related to JSON, Parsing, Serialization, and Deserialization.
Make sure you practice as much as possible and revert your experience.
Got a question for us? Please mention it in the comments section of this Python JSON article and we will get back to you as soon as possible. To get in-depth knowledge of Python along with its various applications, you can enroll here with our live online training with 24/7 support and lifetime access.
Course Name | Date | |
---|---|---|
Data Science with Python Certification Course | Class Starts on 13th February,2023 13th February MON-FRI (Weekday Batch) | View Details |
Data Science with Python Certification Course | Class Starts on 25th February,2023 25th February SAT&SUN (Weekend Batch) | View Details |
edureka.co