0

I'm not really sure what to call this it might be a fairly standard JSON format but I've not encountered it before.

Simplified example:

stock: {
    1223581: {
    shoe_id: 1788,
    id: 123
    }
}

My standard would be something like:

for item in json_data['stock']:
  print item['shoe_id']

But the integer (the 1223581 in this example) is dynamic and it's throwing me.

I've tried:

print item[item]['shoe_id']

Since item[item] was dumping the integer. Also tried:

print item[0]['shoe_id']

I could parse out anything before the ':' and parse it that way but that seems like an ugly fix to what I imagine would be a straightforward problem.

Any suggestions would be much appreciated.

1
  • use get() method of dictionary and check for None , instead of directly specifying the key Commented Dec 1, 2017 at 12:05

1 Answer 1

3

You need to iterate over the items in your json parsed structure - in stock:

stock: {
    1223581: {
        shoe_id: 1788,
        id: 123
    }
}

for item_id, item_spec in item['stock'].items()
    print item_spec['shoe_id']
Sign up to request clarification or add additional context in comments.

Comments

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.