Hello,
<div><br></div><div>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 <a href="http://dev.bitly.com/data_apis.html#v3_realtime_bursting_phrases" target="_blank">/v3/realtime/bursting_phrases</a> 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. </div>
<div><br></div><div>To illustrate, this is the script: </div>
<div><br></div><blockquote style="margin:0 0 0 40px;border:none;padding:0px"><blockquote style="margin:0 0 0 40px;border:none;padding:0px"><div>import urllib2</div></blockquote><blockquote style="margin:0 0 0 40px;border:none;padding:0px">
<div><br></div></blockquote><blockquote style="margin:0 0 0 40px;border:none;padding:0px"><div>CLIENT_ID = "0367d81a428968a57704a295a4378521af81c91b"</div></blockquote><blockquote style="margin:0 0 0 40px;border:none;padding:0px">
<div>CLIENT_SECRET = "404862446e88f391e8c411ca1ee912506d64fffd"</div></blockquote><blockquote style="margin:0 0 0 40px;border:none;padding:0px"><div>ACCESS_TOKEN = "53a01f38b09c0463cb9e2b35b151beb127843bf3"</div>
</blockquote><blockquote style="margin:0 0 0 40px;border:none;padding:0px"><div>BITLY_ENDPOINT = "<a href="https://api-ssl.bitly.com/v3/realtime/bursting_phrases?access_token=" target="_blank">https://api-ssl.bitly.com/v3/realtime/bursting_phrases?access_token=</a>"+ACCESS_TOKEN</div>
</blockquote><blockquote style="margin:0 0 0 40px;border:none;padding:0px"><div><br></div></blockquote><blockquote style="margin:0 0 0 40px;border:none;padding:0px">
<div>def getServiceResponse():</div></blockquote><blockquote style="margin:0 0 0 40px;border:none;padding:0px"><div> url = BITLY_ENDPOINT</div></blockquote><blockquote style="margin:0 0 0 40px;border:none;padding:0px">
<div> request = urllib2.Request(url)</div></blockquote><blockquote style="margin:0 0 0 40px;border:none;padding:0px"><div> response = urllib2.urlopen(request)</div></blockquote><blockquote style="margin:0 0 0 40px;border:none;padding:0px">
<div> d = json.loads(response.read()) </div></blockquote><blockquote style="margin:0 0 0 40px;border:none;padding:0px"><div> </div></blockquote>
<blockquote style="margin:0 0 0 40px;border:none;padding:0px"><div>getServiceResponse()</div></blockquote></blockquote><div><br></div><div>In terminal I run this command: python /file/location/file.py | tr "," "\n" which returns a long list of results like this:</div>
<div> </div><blockquote style="margin:0 0 0 40px;border:none;padding:0px"><blockquote style="margin:0 0 0 40px;border:none;padding:0px"><div>"data": {"selectivity": 3.0</div></blockquote><blockquote style="margin:0 0 0 40px;border:none;padding:0px">
<div>"phrase": "justin bieber"</div></blockquote><blockquote style="margin:0 0 0 40px;border:none;padding:0px"><div> <span class="Apple-tab-span" style="white-space:pre"> </span>"mean": 0.089999999999999997}</div>
</blockquote><blockquote style="margin:0 0 0 40px;border:none;padding:0px"><div> <span class="Apple-tab-span" style="white-space:pre"> </span>{"std": 0.046334721206249076</div></blockquote><blockquote style="margin:0 0 0 40px;border:none;padding:0px">
<div> <span class="Apple-tab-span" style="white-space:pre"> </span>"ghashes": [</div></blockquote><blockquote style="margin:0 0 0 40px;border:none;padding:0px"><div><span class="Apple-tab-span" style="white-space:pre"> </span> {"visitors": 440</div>
</blockquote><blockquote style="margin:0 0 0 40px;border:none;padding:0px"><div><span class="Apple-tab-span" style="white-space:pre"> </span> "ghash": "QXfZfQ"}</div></blockquote><blockquote style="margin:0 0 0 40px;border:none;padding:0px">
<div><span class="Apple-tab-span" style="white-space:pre"> </span> {"visitors": 215</div></blockquote><blockquote style="margin:0 0 0 40px;border:none;padding:0px"><div><span class="Apple-tab-span" style="white-space:pre"> </span> "ghash": "W9sHrc"}</div>
</blockquote><blockquote style="margin:0 0 0 40px;border:none;padding:0px"><div><span class="Apple-tab-span" style="white-space:pre"> </span> {"visitors": 92</div></blockquote><blockquote style="margin:0 0 0 40px;border:none;padding:0px">
<div><span class="Apple-tab-span" style="white-space:pre"> </span> "ghash": "XY9PPX"}]</div></blockquote><blockquote style="margin:0 0 0 40px;border:none;padding:0px"><div> <span class="Apple-tab-span" style="white-space:pre"> </span>"N": 203183.0</div>
</blockquote><blockquote style="margin:0 0 0 40px;border:none;padding:0px"><div> <span class="Apple-tab-span" style="white-space:pre"> </span>"rate": 0.53000000000000003</div></blockquote><blockquote style="margin:0 0 0 40px;border:none;padding:0px">
<div> <span class="Apple-tab-span" style="white-space:pre"> </span> <span class="Apple-tab-span" style="white-space:pre"> </span>"urls": [</div></blockquote><blockquote style="margin:0 0 0 40px;border:none;padding:0px">
<div><span class="Apple-tab-span" style="white-space:pre"> </span> {"visitors": 440</div></blockquote><blockquote style="margin:0 0 0 40px;border:none;padding:0px"><div><span class="Apple-tab-span" style="white-space:pre"> </span> "aggregate_url": "<a href="http://bit.ly/QXfZfQ">http://bit.ly/QXfZfQ</a>"}</div>
</blockquote><blockquote style="margin:0 0 0 40px;border:none;padding:0px"><div><span class="Apple-tab-span" style="white-space:pre"> </span> {"visitors": 215</div></blockquote><blockquote style="margin:0 0 0 40px;border:none;padding:0px">
<div><span class="Apple-tab-span" style="white-space:pre"> </span> "aggregate_url": "<a href="http://bit.ly/W9sHrc">http://bit.ly/W9sHrc</a>"}</div></blockquote><blockquote style="margin:0 0 0 40px;border:none;padding:0px">
<div><span class="Apple-tab-span" style="white-space:pre"> </span> {"visitors": 92</div></blockquote><blockquote style="margin:0 0 0 40px;border:none;padding:0px"><div><span class="Apple-tab-span" style="white-space:pre"> </span> "aggregate_url": "<a href="http://bit.ly/XY9PPX">http://bit.ly/XY9PPX</a>"}]</div>
</blockquote></blockquote><div><br></div>How do I use json to filter out the "ghashes" from what is printed into terminal so that the results look like this instead?:<div><br></div><blockquote style="margin:0 0 0 40px;border:none;padding:0px">
<div><blockquote style="margin:0px 0px 0px 40px;border:none;padding:0px">"data": {"selectivity": 3.0</blockquote></div><div><blockquote style="margin:0px 0px 0px 40px;border:none;padding:0px">"phrase": "justin bieber"</blockquote>
</div><div><blockquote style="margin:0px 0px 0px 40px;border:none;padding:0px"> <span class="Apple-tab-span" style="white-space:pre"> </span>"mean": 0.089999999999999997}</blockquote></div><div><blockquote style="margin:0px 0px 0px 40px;border:none;padding:0px">
<span class="Apple-tab-span" style="white-space:pre"> </span>{"std": 0.046334721206249076</blockquote></div><div><blockquote style="margin:0px 0px 0px 40px;border:none;padding:0px"> <span class="Apple-tab-span" style="white-space:pre"> </span>"N": 203183.0</blockquote>
</div><div><blockquote style="margin:0px 0px 0px 40px;border:none;padding:0px"> <span class="Apple-tab-span" style="white-space:pre"> </span>"rate": 0.53000000000000003</blockquote></div><div><blockquote style="margin:0px 0px 0px 40px;border:none;padding:0px">
<span class="Apple-tab-span" style="white-space:pre"> </span> <span class="Apple-tab-span" style="white-space:pre"> </span>"urls": [</blockquote></div><div><blockquote style="margin:0px 0px 0px 40px;border:none;padding:0px">
<span class="Apple-tab-span" style="white-space:pre"> </span> {"visitors": 440</blockquote></div><div><blockquote style="margin:0px 0px 0px 40px;border:none;padding:0px"><span class="Apple-tab-span" style="white-space:pre"> </span> "aggregate_url": "<a href="http://bit.ly/QXfZfQ">http://bit.ly/QXfZfQ</a>"}</blockquote>
</div><div><blockquote style="margin:0px 0px 0px 40px;border:none;padding:0px"><span class="Apple-tab-span" style="white-space:pre"> </span> {"visitors": 215</blockquote></div><div><blockquote style="margin:0px 0px 0px 40px;border:none;padding:0px">
<span class="Apple-tab-span" style="white-space:pre"> </span> "aggregate_url": "<a href="http://bit.ly/W9sHrc">http://bit.ly/W9sHrc</a>"}</blockquote></div><div><blockquote style="margin:0px 0px 0px 40px;border:none;padding:0px">
<span class="Apple-tab-span" style="white-space:pre"> </span> {"visitors": 92</blockquote></div><div><blockquote style="margin:0px 0px 0px 40px;border:none;padding:0px"><span class="Apple-tab-span" style="white-space:pre"> </span> "aggregate_url": "<a href="http://bit.ly/XY9PPX">http://bit.ly/XY9PPX</a>"}]</blockquote>
</div></blockquote><div><br></div>Any help would greatly be appreciated, thank you!<div><br></div><div>- Jasmine<br><div><br></div></div>