The world’s leading publication for data science, AI, and ML professionals.

Investigating the JSON Module

Exploring one of the most popular data formats in Python

Image Source
Image Source

Json, which stands for JavaScript Object Notation, was created with the intention of helping make data transportation more efficient. It has done just that, and it is now the gold standard for data transfers on the web. In this blog post, I am going to go through how to utilize the data stored within JSON files in Python using the JSON module.

First, we want to load our JSON file. The file I’ll be going through in this post contains New York City campaign finance data from 2001. As noted by the title of this post, there is a very useful JSON module in Python that can be imported super easily that makes handling JSON files much easier.

import json

To load a JSON file, you first open the file using Python’s built-in open function. Then, you would pass that file object to the JSON module’s load method. Once you’ve done that, it’s good to check and see what kind of datatype the JSON file has been loaded as. What datatype do you think it will be?

If you guessed a dictionary, great job! JSON files are usually created in a hierarchical, nested structure, meaning they can be confusing and maybe even overwhelming at first. But, many datatypes in JSON have equal partners in Python, just with different names. For example:

  • Python list, tuple = JSON array
  • Python str = JSON string
  • Python float, int = JSON number
  • Python True = JSON true
  • Python False = JSON false
  • Python None = JSON null

And because of the awesome developers of the JSON module, you can use a lot of the same methods you are familiar with in Python to investigate your JSON file. Some examples:

  • Checking the keys of a dictionary:
  • Checking the datatypes for your values:
  • Viewing a dictionary as a DataFrame:
  • Seeing the number of values inside of a key:
  • And previewing data:

So while the structure of JSON files may be a bit intimidating, the JSON module in Python allows you to do your EDA in a way that is efficient and comfortable for those familiar with Python. If you want to gain some practice on how to investigate nested data, JSON files are a great source to do that.

Thank you for reading!

LinkedIn


Related Articles