Mastering Python (98 Blogs) Become a Certified Professional
AWS Global Infrastructure

Data Science

Topics Covered
  • Business Analytics with R (32 Blogs)
  • Data Science (26 Blogs)
  • Mastering Python (93 Blogs)
  • Decision Tree Modeling Using R (1 Blogs)
SEE MORE

What is Python JSON and How to implement it?

Published on Aug 02,2019 10.9K Views

47 / 62 Blog from Python Fundamentals

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:

Introduction to JSON in Python:

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.

JSON logo- Python JSON-Edureka

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;

How to read a JSON file in Python?

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.

Example:

 import json

Now that you are aware of JSON in Python, let’s take a deeper look at Parsing.

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:

  1. Conversion from JSON to Python
  2. Conversion from Python to JSON

Let’s get a better understanding of both stages.

Conversion from JSON to Python:

You can convert JSON string to Python by using json.loads(). Let me show you the practical implementation:

Example:

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)

Output:

Output - Python JSON - Edureka

As you can see from the above output, it has printed a Python dictionary. Let’s print the datatype for better understanding.

Example:

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

Output:

<class ‘dict’>

Now, that you are familiar with one conversion, let’s see the other conversion type in the second stage.

Conversion from Python to JSON:

A Python object can be converted to JSON string by using json.dumps(). Let’s take a look at an example given below:

Example:

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)

Output:

 

Python-to-JSON-Python JSON-Edureka

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.

Pandas Parsing JSON:

JSON string can be parsed into a pandas Dataframe from the following steps:

  • The following generic structure can be used to load the JSON string into the DataFrame.
import pandas as pd

pd.read_json(r'Path where you saved the JSON fileFile Name.json')
  • Prepare the JSON string.
  • Create a JSON file which we are using is nobel_prize.json.
  • Load the JSON file into pandas DataFrame.

    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)

    Output:

    Parse-Pandas-JSON-Python JSON-Edureka

     

    Moving ahead, let us see how you can serialize JSON in Python.

    Serialization of JSON [Encode]:

    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.

    PythonJSON

    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:

    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.

    Example:

    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:
    Pretty-printing-Python JSON-Edureka

    Moving ahead in Python JSON tutorial, let us understand the deserialization of JSON.

    Deserialization of JSON [Decode]:

    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.

    JSONPython

     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.

    Coding Demonstration:

    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.

    Example (Serialization of JSON dataset):

    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)

    Output:

    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”.

    Example(Deserialization of JSON dataset):

    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'])

    Output:

    The code snippet shows the changes from a JSON file to its respective Python object.

    Output - Python JSON - Edureka

    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.

      Upcoming Batches For Data Science with Python Certification Course
      Course NameDate
      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
      Comments
      0 Comments

      Join the discussion

      Browse Categories

      webinar REGISTER FOR FREE WEBINAR
      REGISTER NOW
      webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP

      Subscribe to our Newsletter, and get personalized recommendations.

      image not found!
      image not found!

      What is Python JSON and How to implement it?

      edureka.co