Search code examples
pythonstringdictionarypython-itertools

How to access value of nested dictionary with only subkey?


I have the following nested dictionaries:

{
  "category": "string",
  "top_bottom": {
    "count": 0,
    "data": [
      {
        "store_name": "string",
        "store_id": "string",
        "value": 0
      }
    ],
    "end_name": "string"
  }
}

And I want to get the value for the key store_name. How can I do that?


Solution

  • You'll always need to know the "full path" to get there. So if your dictionary is called myDict, for instance, you'd get there with

    myDict["top_bottom"]["data"][0]["store_name"]
    

    If you have no way of knowing the "full path," it may be a good idea to consider a different structure for your data.