Hone Your Powers – Part 2 (JSON)

In this challenge, we are required to perform some data parsing using JSON methods. Since I am going to use Python, I will be importing some libraries into my file and then make some real API calls to Twitter instead of using the provided JSON data.

I am going to do several things and still make this post as short as possible!

[python]
#perform some data parsing using json.
#make api calls to twitter servers
#### mozilla challenge – programming with twitter api

import urllib2 #python module for handling urls
import json #json anyone??

url = ‘http://api.twitter.com/1/statuses/user_timeline/eenvincible.json’

j_data = urllib2.urlopen(url) #fetch the data from twitter
processed_data = json.load(j_data) #use json to parse data
print len(processed_data) #prints length of the data 4 fun

#extract specific key:value pairs from the dictionaries
#a while loop is an option here with a counter variable i
i = 0
while i < len(processed_data):
print processed_data[i][‘text’].encode(‘utf-8’)
i += 1

#####what do you expect from that loop? ##############

##Hone Your Powers – Programming 101 http://t.co/QZ1nHUeJ
##And thou shalt love the Lord thy God with all thine heart, and wi#th all #thy #soul, and with all thy might. http://t.co/lmTHrGiH
##It is of the Lord’s mercies that we are not consumed, because his #compassions fail not.They are new every morni… http://t.co/EhqESH#g5
##@kamajowa you made this year start right and I wish you the best #too #friend
##It is of the Lord’s mercies that we are not consumed, because his #compassions fail not.They are new every morni… http://t.co/EhqESH#g5
##@StandardKenya that guy to the right appears out of place though. #Look #at his eye
##Happy new year @TVandenbosch @keikamara @upgrademycredit #@GraceM#waura #@#jsonmez @kamajowa @cchums @kellybetterkc 2013
#@kellybetterkc happy new year
##It is 2013 in New York and still 2012 in the Midwest
##I have invested quality time and money into understanding things #but #stupidity still surprises me. Happy new year people
##2013 is just another year. The difference between this year and t#omorrow #is time. Do something useful and stop worrying bout resol#u#tions

[/python]

As you can see, this is a good step towards turning JSON data returned by API calls into useful information. So, with a few lines of python code combined with JSON, we have coverted this:

returnedjsonfromtwitter

into very easily readable tweets as shown below:

twitterdata

So, I just used one key (‘text’) to pull up tweets but there are so many other keys that you could access. Try it using a for loop instead of a while loop. It is so much fun.

Just before I end this post, let me show you how you could access the other keys in the dictionaries that might be inside other dictionaries!

[python]
#assuming you have the data returned from twitter and you
#know what is contained in that data [dictionaries]
#then you can easily loop through the list looking for stuff:
#consider our original data stored in [processed_data]

#locate the 5th dictionary, the key ‘user’ n the key ‘description’
#which is in another dictionary!
print processed_data[5][‘user’][‘decription’]

##output will be: depends on screen_name used in the call####
#Software Enthusiast, Playing Piano & a Published Author Title: My #Life #Sentences – A True Story!

print processed_data[5][‘source’] #source of tweet

###output from my tweets:

##<a href=”http://twitter.com/download/android” rel=”nofollow”>Twit#t##er for Android</a>

print processed_data[5][‘user’][‘verified’]

##output will be …. wait for it ……

#False

#Try a for loop.

for key in processed_data[0]:
if key == ‘text’:
print processed_data[0][key] #print the tweet
[/python]

So, there you have it! You can easily step through that json data and pull out whatever you want!

That should do it for now. Please let me know if you have any questions. If you have any blogs, don’t hesitate to share a link because I would like to check it out! Thanks for visiting and see you around and hopefully this helped you learn something fun!

Be The First To Get New Posts From Simple Developer…

Written By Elisha Chirchir

Elisha Chirchir is a software developer. He is also the founder of Simple Developer and co-founder of Instinctive Software Solutions. On any given day, he works on both Android and Web Development. During his 'free time', he offers training to those interested in learning how to code in php, java, python, javaScript etc. You can easily find him on StackOverflow Android chatroom or on Twitter @Eenvincible

Leave a Reply

Your email address will not be published. Required fields are marked *