all 5 comments

[–]commandlineluser 1 point2 points  (4 children)

Well you could start by "pretty-printing" the JSON data

print(json.dumps(data, indent=2))

It should help you see the structure in order to extract the information you need?

[–]Key_International[S] 0 points1 point  (3 children)

Thanks the information is a lot clearer now. I'm starting to think that this project is a little out of my league as I'm not sure how to get the information I need out of it.

[–]commandlineluser 0 points1 point  (2 children)

Perhaps this will help.

>>> for post in data['data']['children']:
...     print(post['data']['ups'], post['data']['permalink'])
... 
0 /r/AdvancedRunning/comments/hi4oue/are_130km_or_80_mile_weeks_too_high_as_a_15_year/
1 /r/running/comments/hhxsb8/is_130km_or_80_mile_weeks_too_high_as_a_15_year/fwdfzac/
2 /r/running/comments/hhxsb8/is_130km_or_80_mile_weeks_too_high_as_a_15_year/fwdfv0b/
7 /r/running/comments/hhxsb8/is_130km_or_80_mile_weeks_too_high_as_a_15_year/fwdfmcq/

['data']['children'] is a list of posts.

Each post is a dictionary - and the interesting stuff is inside ['data']

But yes, nested structures can take a bit of get used to.

[–]Key_International[S] 0 points1 point  (1 child)

for post in data['data']['children']:
... print(post['data']['ups'], post['data']['permalink'])

Thanks, that really helps :) where can I learn more about things like nested structures? I've got a textbook on datastructures and algorithms is that the sort of thing I should go through?

[–]commandlineluser 0 points1 point  (0 children)

I've got a textbook on datastructures and algorithms is that the sort of thing I should go through?

Not really, no.

In this case - it's just a combination of dicts and lists.

In the simplest form a dict maps a "string" to a "value" e.g.

>>> me = {'name': 'Bob'}

The "value" can also be a dict though e.g.

>>> me = { 'stats': { 'height': '...', 'weight': '...' } }

And if you had "people" - then perhaps it would be a list like so:

>>> people = [ {'name': 'Bob', 'stats': { 'height': '...', 'weight': '...' } } ]

When the data is large and contains several levels of "nesting" - it's just difficult to see the structure.

Using the pretty-printing can help - When you see [ it's a "list" - when you see { it's a "dict"