Hello, I've just started programming in python and haven't had much experience with json. I've written this simple script that makes a request to the Bitly API, specifically the /v3/realtime/bursting_phrases<http://dev.bitly.com/data_apis.html#v3_realtime_bursting_phrases>endpoint, and prints the response into terminal. What I want to do is take the response and filter out a portion of it so I only see the data I find important. So far I've used json to load the results into a dictionary, however I'm stuck in terms of how you loop through the dictionary and only print out certain keys while dumping the rest.
To illustrate, this is the script: import urllib2 CLIENT_ID = "0367d81a428968a57704a295a4378521af81c91b" CLIENT_SECRET = "404862446e88f391e8c411ca1ee912506d64fffd" ACCESS_TOKEN = "53a01f38b09c0463cb9e2b35b151beb127843bf3" BITLY_ENDPOINT = " https://api-ssl.bitly.com/v3/realtime/bursting_phrases?access_token= "+ACCESS_TOKEN def getServiceResponse(): url = BITLY_ENDPOINT request = urllib2.Request(url) response = urllib2.urlopen(request) d = json.loads(response.read()) getServiceResponse() In terminal I run this command: python /file/location/file.py | tr "," "\n" which returns a long list of results like this: "data": {"selectivity": 3.0 "phrase": "justin bieber" "mean": 0.089999999999999997} {"std": 0.046334721206249076 "ghashes": [ {"visitors": 440 "ghash": "QXfZfQ"} {"visitors": 215 "ghash": "W9sHrc"} {"visitors": 92 "ghash": "XY9PPX"}] "N": 203183.0 "rate": 0.53000000000000003 "urls": [ {"visitors": 440 "aggregate_url": "http://bit.ly/QXfZfQ"} {"visitors": 215 "aggregate_url": "http://bit.ly/W9sHrc"} {"visitors": 92 "aggregate_url": "http://bit.ly/XY9PPX"}] How do I use json to filter out the "ghashes" from what is printed into terminal so that the results look like this instead?: "data": {"selectivity": 3.0 "phrase": "justin bieber" "mean": 0.089999999999999997} {"std": 0.046334721206249076 "N": 203183.0 "rate": 0.53000000000000003 "urls": [ {"visitors": 440 "aggregate_url": "http://bit.ly/QXfZfQ"} {"visitors": 215 "aggregate_url": "http://bit.ly/W9sHrc"} {"visitors": 92 "aggregate_url": "http://bit.ly/XY9PPX"}] Any help would greatly be appreciated, thank you! - Jasmine
_______________________________________________ Tutor maillist - [email protected] To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
