1

I am attempting to parse this JSON file. I need to be able to access and preform a download using the contents of each hash entry. How can I do this in Python without having to manually write code to access each and every entry?

2
  • 1
    By using a JSON library? Jan 8, 2014 at 22:55
  • @RobertHarvey - I meant how do I do it automatically without having to write code to parse each and every entry.
    – Nathan2055
    Jan 9, 2014 at 4:13

2 Answers 2

4

You'd use the json library to parse the data.

You must first load the data from the web and decode it to a Unicode string:

import json
from urllib.request import urlopen

response = urlopen('https://s3.amazonaws.com/Minecraft.Download/indexes/legacy.json')
# the default encoding for JSON is UTF-8, but the response can give you
# a different codec
encoding = response.info().get_content_charset('utf-8')

data = json.loads(response.read().decode(encoding))

Now you can loop through the data:

for name, info in data['objects'].items():
    print('{}: {}'.format(name, info['hash']))

This produces:

lang/fr_CA.lang: 6df06576e677d952dc15da3926f4ed822d106354
sounds/random/orb.ogg: e9833a1512b57bcf88ac4fdcc8df4e5a7e9d701d
sounds/mob/villager/yes1.ogg: be73a79fb1ab966887a8b88bed9aa6b3d9173d71
sounds/mob/cat/purreow2.ogg: 08573a1f11058b09c5855122dff47ceb209f771e
sound/mob/spider/say2.ogg: 501b40b97ee55cb7a97943ee620aa05131089fc2
lang/el_GR.lang: 4330e3218548e9f3268d103e5ab71fa2b28d9b20
sound/mob/horse/soft1.ogg: a9a83e3f186b3af56a9844b8d4976c6251ba17eb
sounds/mob/bat/death.ogg: 6df3b2c2b951863fc5200edf3ff3396b105ed897
# etc.
0
0

I guess the short answer would be the json module.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.