You are on page 1of 5

CMP6221: Computing for AI

Lab Session 6: JSON Data in Python

1. Objectives
a. Converting Python objects to JSON
b. Reading and writing a JSON Files

Note: Although most of the instructions in this sheet are written for command line interpreter,
you are advised to save your python codes as scripts.

1. Introduction

JSON(JavaScript Object Notation) is a lightweight data-interchange format that easy for


humans to read and write. It is also easy for computers to parse and generate. JSON is
based on the JavaScript programming language. It is a text format that is language
independent and can be used in Python, Perl among other languages. It is primarily used
to transmit data between a server and web applications. JSON is built on two structures:

A collection of name/value pairs. This is realized as an object, record, dictionary, hash


table, keyed list, or associative array.

An ordered list of values. This is realized as an array, vector, list, or sequence.

2. Converting JSON to Python Objects

We can parse the above JSON string using json.loads() method from the json module. The
result is a Python dictionary.
3. Converting Python Objects to JSON
Using json.dumps() we can convert Python Objects to JSON.

Let's compare the data types in Python and JSON.

Python JSON

dict Object
Python JSON

list Array

tuple Array

str String

int Number

float Number

True true

False false

None null

Below we'll show how you can convert various Python objects to different JSON data types.

Python Tuple to JSON Array

Python List to JSON Array

Python String to JSON String

Python Boolean Values to JSON Boolean Values


4. Writing a JSON File

The json module also allows us to write JSON data into a JSON file. JSON files are saved
with the .json extension. Let's see how we can do that below. In order to achieve this, we
use Python's open() function with w as the parameter to signify that we want to write the
file.

5. Reading JSON Files


Now let's show how we can read in the JSON file we just created. We use json.load to load
in files.

json.load vs json.loads

json.load is used when loading a file while json.loads(load string) is used when loading a
string.
json.dump vs json.dumps
We use json.dump when we want to dump JSON into a file. json.dumps(dump string) is
used when we need the JSON data as a string for parsing or printing.

6. Handling JSON Data in Data Science


Sometimes we need to load in data that is in JSON format during our data science activities.
Pandas provides .read_json that enables us to do this. Once the data is loaded, we convert
it into a dataframe using the pandas.DataFrame attribute.

7. Implementation Limitations

The process of encoding JSON is called Serialization while the process of decoding
JSON is called deserialization. Some JSON deserializer implementations may set limits
on:

• The size of accepted JSON texts


• The maximum level of nesting of JSON objects and arrays
• The range and precision of JSON numbers
• The content and maximum length of JSON strings

However such limitations are only those relevant to Python data types and the Python
interpreter itself.

8. Exercise
For this exercise, we will keep track of when our friend’s birthdays are, and be able to find
that information based on their name. Create a dictionary (in a JSON file) of names and
birthdays. When you run your program it should ask the user to enter a name, and return
the birthday of that person back to them. The interaction should look something like this:

>>> Welcome to the birthday dictionary. We know the birthdays of:


Albert Einstein
Benjamin Franklin
Ada Lovelace
>>> Who's birthday do you want to look up?
Benjamin Franklin
>>> Benjamin Franklin's birthday is 01/17/1706.

You might also like