6

I have the following JSON string (from wikipedia http://en.wikipedia.org/wiki/JSON)

{
        "name":"Product",
        "properties":
        {
                "id":
                {
                        "type":"number",
                        "description":"Product identifier",
                        "required":true
                },
                "name":
                {
                        "type":"string",
                        "description":"Name of the product",
                        "required":true
                },
                "price":
                {
                        "type":"number",
                        "minimum":0,
                        "required":true
                },
                "tags":
                {
                        "type":"array",
                        "items":
                        {
                                "type":"string"
                        }
                },
                "stock":
                {
                        "type":"object",
                        "properties":
                        {
                                "warehouse":
                                {
                                        "type":"number"
                                },
                                "retail":
                                {
                                        "type":"number"
                                }
                        }
                }
        }
}    

I am trying to decode this string using Python json library. I would like to access the node

properties - > stock - > properties - > warehouse.

I understand that json.loads() function stores the json string as a dictionary. But in this case properties is my key and everything under that are values. How do I access the above node.

import json
jsonText=""
file = open("c:/dir/jsondec.json")
for line in file.xreadlines():
    jsonText+=line
data = json.loads(jsonText)
for k,v in data.items():
    print k // shows name and properties
file.close();

Thanks

1
  • Are you aware of json.load()? Commented Jun 20, 2012 at 19:39

1 Answer 1

18

You can load json straight from the file like this:

 f = open("c:/dir/jsondec.json")
 data = json.load(f)

Based on your input string, data is now a dictionary that contains other dictionaries. You can just navigate up the dictionaries like so:

 node = data['properties']['stock']['properties']['warehouse']
 print str(node)
Sign up to request clarification or add additional context in comments.

3 Comments

@Ankur sometimes your json won't come from a file - that's when you use loads.
Thanks.. In works for this json but in other json I get TypeError: list indices must be integers, not str. Can you guess why
The data structure you get back will entirely depend on the json you put in - the elements of your dictionaries might be scalar types, lists or dictionaries. If you have more generic iteration requirements, it might be worth editing your question.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.