Tuesday, December 4, 2018

Storing Data using JSON

Python's json module allows us to dump simple Python data structures into a file and load the data from that file the next time the program runs. Often our programs will ask users to input certain kinds of information or allow users to store preferences in a game or provide data for a visualization. Thus we’ll store the information users provide in data structures such as lists and dictionaries and when users close a program, we always want to save the information they entered. A simple way to do this involves storing your data using the json module. We can also use json to share data between different Python programs and even programs in other programming languages.

JavaScript Object Notation is a popular way to format data as a single human-readable string. JSON is the native way that JavaScript programs write their data structures. as accessing any other web page via a URL. The difference is that the data returned by an API is formatted (with JSON, for example) for machines; APIs aren’t easy for people to read.

Many websites make their data available in JSON format. Facebook, Twitter, Yahoo, Google, Tumblr, Wikipedia, Flickr, Data.gov, Reddit, IMDb, Rotten Tomatoes, LinkedIn, and many other popular sites offer APIs for programs to use.

We have to find documentation for what URLs your program needs to request in order to get the data you want, as well as the general format of the JSON data structures that are returned. This documentation should be provided by whatever site is offering the API; if they have a “Developers” page, look for the documentation there.

Using APIs, you could write programs that do the following:

• Scrape raw data from websites. (Accessing APIs is often more convenient than downloading web pages and parsing HTML with Beautiful Soup.)

• Automatically download new posts from one of your social network accounts and post them to another account. For example, you could take your Tumblr posts and post them to Facebook.

• Create a “movie encyclopedia” for your personal movie collection by pulling data from IMDb, Rotten Tomatoes, and Wikipedia and putting it into a single text file on your computer.

The json Module

Python’s json module handles all the details of translating between a string with JSON data and Python values for the json.loads() and json.dumps() functions. JSON can’t store every kind of Python value. It can contain values of only the following data types: strings, integers, floats, Booleans, lists,
dictionaries, and NoneType. JSON cannot represent Python-specific objects, such as File objects, CSV Reader or Writer objects, Regex objects, or Selenium WebElement objects.

Now let's write a program that stores a set of odd numbers and another program that reads these numbers back into memory. The first program will use json.dump() to store the set of odd numbers, and the second program will use json.load().

The json.dump() function takes two arguments: a piece of data to store and a file object it can use to store the data. See the code below:

import json

odd_numbers = [1,3,5,7,9]

filename = 'odd_numbers.json'

with open (filename,'w')as file_obj:
   
    json.dump(odd_numbers,file_obj) 


In the preceding code we have a list odd_numbers containing some odd numbers. Next we define a filename in JSON format, 'odd_numbers.json', in which the odd_numbers list will be stored. Now we open the file in write mode and use the json.dump() function to store the odd_numbers list in the 'odd_numbers.json' file.


When we run the program no output can be seen but upon opening the odd_numbers.json file we can see the content of the list odd_numbers.



JSON module's load() is used to read the list back into the memory. The following program demonstrates this:

import json

filename = 'odd_numbers.json'

with open(filename)as file_obj:
   
    odd_number_list = json.load(file_obj)
   
print(odd_number_list)


In the preceding program we open 'odd_numbers.json' file we created in the previous program and using json.load() function, load it's information in the variable odd_number_list. To see the loaded information we print the content of odd_number_list variable. The output is shown below:






Reading JSON with the loads() Function

To translate a string containing JSON data into a Python value, pass it to the json.loads() function. (The name means “load string,” not “loads.”). The following program demonstrates this:

import json

new_book = '{"author":"Harris","subject":"python","title":"python for beginners"}'

content = json.loads(new_book)

print(content)



We have a dictionary new_book which is a string of JSON data. Note that JSON strings always use double quotes. It will return that data as a Python dictionary. Next we call loads() and pass it a string of JSON data i.e new_book and store this in variable content. Finally we print the variable content to see information stored in the variable. Python dictionaries are not ordered, so the key-value pairs may appear in a different order when we print the variable content. The output is shown below:




Writing JSON with the dumps() Function

The json.dumps() function (which means “dump string,” not “dumps”) will translate a Python value into a string of JSON-formatted data. The following program demonstrates this:

import json

new_book = {'author': 'Harris', 'subject': 'python', 'title': 'python for beginners'}

contentinjson = json.dumps(new_book)

print(contentinjson)


In the preceding program we have a Python dictionary new_book. Using json.dumps() function we translated this Python value new_book into a string of JSON-formatted data which is stored in the variable contentinjson. At last we print the information stored in the contentinjsonvariable. In the output shown below note that JSON strings always use double quotes. It will return that data as a Python dictionary.


The constraint with these functions is that the value can only be one of the following basic Python data types: dictionary, list, integer, float, string, Boolean, or None.

JSON with user-generated data

Saving data with json is useful when we’re working with user-generated data, because if we don’t store our user’s information somehow, we’ll lose it when the program stops running.

Let’s look at an example where we prompt the user for their name the first time they run a program and then remember their name when they run the program again.

import json

emp_name = input("Enter your name: \n")

filename = 'emp_name.json'

with open (filename,'w')as file_obj:
  
    json.dump(emp_name,file_obj)

    print("\nYour are in our database now " + emp_name)  

Our program prompts an employee to enter the name which is stored in emp_name variable. Then we defined a filename to contain the information of emp_name in json format. Using json.dump() with emp_name and file object as parameters we store the emp_name variable's information in  emp_nam.json file. The print statement prints an information stored message. The output is shown below:




Now it's time to verify that the information we entered through the previous program was stored and can be used. See the program below:

import json

filename = 'emp_name.json'

with open (filename)as file_obj:
  
    emp_name = json.load(file_obj)
    print("\nHow may we help you " + emp_name)


In the preceding program we used json.load() to read the information stored in the emp_name.json into the variable emp_name. Later we printed the information retrieved which proves that the the information we entered through the previous program was stored and can be used. See the output below:



As a practice and revision, use exception handling for the programs from this post. I'll end today's discussion now and till we meet next keep learning and practicing Python as Python is easy to learn!












Share:

0 comments:

Post a Comment