Thursday, October 3, 2013

Parse JSON using Python and store in MySQL

JSON is one the most widely used data format. If you are trying to gather some data using any API then most probably you are going to deal with JSON. Lets see how to parse JSON and get specific parameter values.

Python has a library called "json" which will helps us to deal with the json data. Also we can use the "pprint" which is "data pretty printer" to display formatted output. Generally we will have three main steps for this kind of task,
  • Read the JSON data
  • Store values in python variables
  • Store the data in some database

Lets try to write a simple program based on these three steps,

# Import library to deal with json 
import json

# Import library which can print parsed data 
from pprint import pprint

# Open the json file
testFile = open("test.json")

# Load json data in python variable
data = json.load(testFile)

# You can check the type of python data variable, it should be dictionary

# Print the formatted output

[Click on image to see enlarged version]

# Now we can access any element (value) using corresponding key as follow,
# Print value of id_str

# Store few values in python variable
id_str = data["id_str"]
created_at = data["created_at"]
filter_level = data["filter_level"]

# Store nested values
userData = data["user"]
uid = userData["id"]
followers_count = userData["followers_count"]

# Print the values to make sure we are getting the right data
print (id, created_at, filter_level, uid, followers_count)

Now storing independent python variable values in to database is easy task. You can refer this post for the same.

Note: The json data object used here is collected from twitter API.

No comments:

Post a Comment