Search code examples
pythontumblrpytumblr

confused on how to access nested values in json code?


using the pytumblr client i get this type of output

{
"total_blogs": 2,
"blogs": [
  {
    "name": "testblog",
    "title": "testblog",
    "description": "A testblog",
    "url": "https://testblog.tumblr.com/",
    "uuid": "t:g8wqt6wBUe3AJkJXYHn1",
    "updated": 1526680515 
  },
  {
    "name": "testblog1",
    "title": "testblog1",
    "description": "A testblog1",
    "url": "https://testblog1.tumblr.com/",
    "uuid": "t:qwuedBBFIPMTViKhjozp",
    "updated": 1510382395 
  }],
"_links": {
  "next": {
    "href": "/v2/user/following?offset=20",
    "method": "GET",
    "query_params": {
      "offset": "20"
    }
  }
}

} }

i can print the values of total_blogs and blogs just fine, but I'm having trouble accessing the itnernal values, specifically the url and I haven't been able to apply the tutorials or some of the other examples i've seen here in a way that would help with this issue.

The end game is basically just to be able to loop the program until i obtain all of the url values. I can only access of 20 blogs at a time so that's how many url values i will have to get

Accessing json array in python without referring to its name

an answer in this page seemed to be the solution but attempting to apply it to my code

for anything in usrFollowing:
  if isinstance(usrFollowing[anything], list):
    for values in usrFollowing[anything]:
      print(values['blogs']['name'])

just gives me KeyError: 'blogs' from the last line. i'm not sure what else i can do at this point

an additional problem i have is figuring out how to output the code into a more readable format. on the tumblr website console, it outputs like the initial code i've shown above, but all I get is just an ongoing line printing to the console. is there any way to change this?


Solution

  • This should retrieve the blog urls that your tumblr follows:

    import pytumblr
    client = pytumblr.TumblrRestClient(...)  # replace ... with your credentials
    
    usrFollowing = client.following()
    for blog in usrFollowing['blogs']:
        print(blog['url'])
    

    I think the way to understand this structure is one piece at a time.

    The usrFollowing var is a dictionary

    ...
    usrFollowing = client.following()
    for key in usrFollowing.keys():
        print(key)
    
    # outputs:
    #   blogs
    #   _links
    #   total_blogs
    

    So, to access each blog, we can iterate over them using the key blogs:

    ...
    usrFollowing = client.following()
    for blog in usrFollowing['blogs']:
        print(blog)
    # outputs something like:
    #   {u'updated': 1539793245, u'uuid': u't:CwoihvyyOxn8Mk5TUS0KDg', u'title': u'Tumblr Engineering', u'url': u'https://engineering.tumblr.com/', u'name': u'engineering', u'description': u'Dispatches from the intrepid tinkerers behind technology at Tumblr.'}
    #   {u'updated': 1545058816, u'uuid': u't:0aY0xL2Fi1OFJg4YxpmegQ', u'title': u'Tumblr Staff', u'url': u'https://staff.tumblr.com/', u'name': u'staff', u'description': u''}
    

    There are several ways to output objects in a more "human" format, use pprint or convert the object to JSON specifying an indentation amount:

    ...
    
    import pprint
    import json
    
    print('Python pretty-printed')
    for blog in usrFollowing['blogs']:
        pprint.pprint(blog)
    
    print('')
    
    print('JSON pretty-printed')
    for blog in usrFollowing['blogs']:
        print(json.dumps(blog, indent=2))
    # outputs something like:
    #   Python pretty-printed
    #   {u'description': u'Dispatches from the intrepid tinkerers behind technology at Tumblr.',
    #    u'name': u'engineering',
    #    u'title': u'Tumblr Engineering',
    #    u'updated': 1539793245,
    #    u'url': u'https://engineering.tumblr.com/',
    #    u'uuid': u't:CwoihvyyOxn8Mk5TUS0KDg'}
    #   {u'description': u'',
    #    u'name': u'staff',
    #    u'title': u'Tumblr Staff',
    #    u'updated': 1545058816,
    #    u'url': u'https://staff.tumblr.com/',
    #    u'uuid': u't:0aY0xL2Fi1OFJg4YxpmegQ'}
    #   
    #   JSON pretty-printed
    #   {
    #     "updated": 1539793245,
    #     "uuid": "t:CwoihvyyOxn8Mk5TUS0KDg",
    #     "title": "Tumblr Engineering",
    #     "url": "https://engineering.tumblr.com/",
    #     "name": "engineering",
    #     "description": "Dispatches from the intrepid tinkerers behind technology at Tumblr."
    #   }
    #   {
    #     "updated": 1545058816,
    #     "uuid": "t:0aY0xL2Fi1OFJg4YxpmegQ",
    #     "title": "Tumblr Staff",
    #     "url": "https://staff.tumblr.com/",
    #     "name": "staff",
    #     "description": ""
    #   }
    

    These dictionaries have a url key, so you can print them with:

    ...
    usrFollowing = client.following()
    for blog in usrFollowing['blogs']:
        print(blog['url'])
    # outputs something like:
    #   https://engineering.tumblr.com/
    #   https://staff.tumblr.com/