-4

I have a json file I need to extract some values from. I've had a look through the docs and a few other examples but still have no idea how to do it (I'm completely new to dealing with jsons). Here is a bit from the json file

{
"response": {
    "success": 1,
    "current_time": 1436797884,
    "items": {
        "A Color Similar to Slate": {
            "last_updated": 1436796007,
            "quantity": 51,
            "value": 101
        },
        "A Deep Commitment to Purple": {
            "last_updated": 1436796007,
            "quantity": 212,
            "value": 101
        },
        "A Distinctive Lack of Hue": {
            "last_updated": 1436796007,
            "quantity": 141,
            "value": 500
        }
    }
}
}

I need to get each key out eg "A Color Similar to Slate" and I need to get the items quantity and value out aswell for each key.

  • 1
    what did you try?? your code snippets? – Ja8zyjits Jul 13 '15 at 14:41
  • I tried just printing it all out which to be fair did work but as soon as i tried to print out specific values it just failed. with open('file.json','r') as f: data=json.loads(open("file.json").read()) print data – Daniel Prinsloo Jul 13 '15 at 14:42
  • 1
    There's a [pretty sweet json module](https://docs.python.org/2/library/json.html) in the standard library. – NightShadeQueen Jul 13 '15 at 14:42
  • 3
    possible duplicate of [Parsing values from a JSON file in Python](http://stackoverflow.com/questions/2835559/parsing-values-from-a-json-file-in-python) – Tim Jul 13 '15 at 14:43
  • i saw that question but my keys are all different his were the same so how do i extract the individual values for each key? – Daniel Prinsloo Jul 13 '15 at 14:45
  • @TimCastelijns mark it, i think you are correct!! – Ja8zyjits Jul 13 '15 at 14:45
  • 1
    His keys are not the same – Tim Jul 13 '15 at 14:46
  • look at the structure of his json he has [] in his im missing those mine looks like a json of jsons – Daniel Prinsloo Jul 13 '15 at 14:48
  • If you have trouble applying the approach in that question to your own situation, you might find it useful to follow a python tutorial for beginners somewhere on the net – Tim Jul 13 '15 at 14:50

1 Answers1

2

Obviously you didn't read the documentation...

>>> data = json.load(
...  open('file.json')
... )
>>> data['response']['items'].keys()
[u'A Color Similar to Slate', u'A Deep Commitment to Purple', u'A Distinctive Lack of Hue']
bufh
  • 3,153
  • 31
  • 36