A quicky on JSON
Posted on August 15, 2016 in misc
JSON¶
JSON is a data format that is used for storing or exchanging information and exchanging information. But JSON is human readable and easy to parse. And most importantly it is structured.
A simple example of json:
{
"name": "jason",
"age": 24,
"gender":"male"
}
Fundamentally a json can be built using two things:
Key Value pairs:¶
You can also call them as property value pairs. Imagine you are collecting data for a classroom. Then your data should store values of properties related to the classroom or students. Some of properties you can consider are "student_id", "name", "class" etc. The json for it would look something like this:
{
"student_id": "514300234324",
"name":"bran",
"class":"12"
}
list :¶
Some times the value of a property can be a list of values ( not just one value). The below example shows that:
{
"hero": "ironman",
"powers": ["cool", "ironSuit", "intellignet"]
}
nested JSON:¶
A JSON object inside another JSON:
{
"id" : "1234",
"name": {
"firstName": "jason",
"lastName": "b"
}
}
JSON library¶
As you can see any of the above examples are easily readable. Similarly they are also easily parsable. To parse JOSN data in python we shall use the json library.
The json library parses the JSON data from either the files or the strings. It converts the JSON data into a python dictionary and viceversa.
Consider the following JSON string:
json_string = '{ "name": "brian", "age": 24, "gender":"male", "skills": ["programming", "musician"]}'
Lets parse this string using the python's json library:
import json
json_obj= json.loads(json_string)
And we can access the parsed data like this:
print json_obj["name"]
print json_obj["age"]
print json_obj["gender"]
print json_obj["skills"]
data=[]
with open('./streamingData.json', 'r') as jsonFile:
for line in jsonFile:
data.append(json.loads(line))
print "Total number of tweets loaded: ", len(data)
All the tweets with their related information are stored in the list named "data". Lets check the different properties that a tweet has:
print data[0].keys() # properties
print data[0]["text"] # how to access the tweet message itself
This is all the properties for one tweet. But for now we will only need actual tweet text and can ignore the rest of the other proprerties.
tweets = []
for item in data:
if "text" in item.keys():
tweet = item["text"]
tweets.append(tweet)
print "total no of tweets extracted from json: ", len(tweets)
There is difference between the json objects count and tweets count becuase, not all json objects have tweets, some might just be empty.
To do:¶
Work on the twitter data that you have extracted in previous chapters:
- Try load the json from file into memory
- Check out the various keys for any one json object, there are some intresting data fields that can be used for different projects.
- Try extract the tweets alone from this json dataset.