Check whether the JSON (object property exists) & print it as unicode decoded
You don't need the intricate tests on wether 'text' is present for the post caption.This code works well with the JSON string you posted:
for post in data['data']: if post.get('caption'): print post['caption'].get('text', 0)
Furthermore, you could be more defensive and refer to data.get('data', [])
when starting the loop in case Instagram sends you empty JSON.
Basically when json loads and deserializes your object, null
in JSON will become None
in python.
So your line of:
if post['caption'] is not 'null':
Should become:
if post['caption']: