r/learnpython • • Nov 02 '25

Most efficient way to find a key/value in a deeply nested Dictionary?

I'm learning API and Json, and I'm having trouble parsing through the data.

Because the returned JSON is very badly formatted

{"coord": {"lon": 139.6917, "lat": 35.6895}, "weather": [{"id": 804, "main": "Clouds", "description": "overcast clouds", "icon": "04d"}], "base": "stations", "main": {"temp": 18.68, "feels_like": 18.17, "temp_min": 17.03, "temp_max": 19.33, "pressure": 1012, "humidity": 60, "sea_level": 1012, "grnd_level": 1010}, "visibility": 10000, "wind": {"speed": 2.72, "deg": 62, "gust": 2.56}, "clouds": {"all": 100}, "dt": 1762049602, "sys": {"type": 2, "id": 268395, "country": "JP", "sunrise": 1762031030, "sunset": 1762069540}, "timezone": 32400, "id": 1850144, "name": "Tokyo", "cod": 200}

 

Brehs... I just want to get the sky clearance and temperature.

So what I do now is I run this through ChatGPT and ask the AI to make it readable.

I do not ask chatgpt to spoonfeed me the index, just make it readable like so

https://i.imgur.com/U49dEA9.png

And from there I just manually try to understand the nesting index

But it still feels like cheating.

 

Is there a smarter way to do this? An easier way to just get the value without having it feel like sifting through a haystack?

Thanks

0 Upvotes

29 comments sorted by

2

u/Yelebear Nov 02 '25

I have to clear something up.

I know how to index through nested dictionaries.

This is how my code looked like

import requests
from datetime import datetime

current_time = datetime.now()
api_key = "9a311fd6832dca1fc646b098cb3bd10b"

user_input = input("Enter City: ").capitalize()

weather_call = requests.get(f"https://api.openweathermap.org/data/2.5/weather?q={user_input}&units=metric&APPID={api_key}")

sky = weather_call.json()["weather"][0]["description"]
temperature = weather_call.json()["main"]["temp"]
wind_speed = weather_call.json()["wind"]["speed"]

print(f"\nLocation: {user_input}")
print(f"As of {current_time}")
print(f"The sky will be {sky}")
print(f"The temperature is {temperature}")
print(f"The wind speeds are {wind_speed}\n")

But what I was asking for is how to make it easier to access nested key:value without having to manually go through it like ["weather"][0]["description"], so I wouldn't have to manually check which key:value is nested where.

Something like .get(), but works for nested values and not just top level.

3

u/Fun-Block-4348 Nov 02 '25

api_key = "9a311fd6832dca1fc646b098cb3bd10b"

Never share personal information like an API key on the internet, always redact it so it can't be used to access the service because some services let you make account modifications/see personal information like name, address, etc with an API key.

1

u/Yelebear Nov 02 '25

Alright. I will next time.

Thanks

3

u/cspinelive Nov 02 '25

And if you are using source control like GitHub, don’t check it into the repo. Pull it from an environment variable in you code instead. 

3

u/TheBB TheBB Nov 02 '25 edited Nov 02 '25

You can go look for a JSONPath library. But there's no built in way to do this. And three levels of indexing is honestly not bad.

Just do like the rest of us: make a function to do the dirty stuff for you so the main logic can be clean-looking.

2

u/pachura3 Nov 02 '25

If dicts are nested, then the same key can be present multiple times , on multiple levels...

2

u/shisnotbash Nov 02 '25

You can use get with a default. Then call get on that: foo.get(“bar”, {}).get(“bar”, []). You can also look into JSON path for querying through JSON. Another option is something like this

try:
  return foo[“bar][“baz”][0]
except (KeyError, IndexError):
  print(“not found”)
 return None

2

u/[deleted] Nov 02 '25 edited Nov 02 '25

Let me get this straight, you want to provide the key temperature and automatically retrieve from ["main"]["temp"]? You need to write your own custom logic to handle these magic values, python doesn't know how the dictionary is organized unless you tell it that temperature equates to ["main"]["temp"].

Wrap the dict in a custom Weather class and write a custom .get() method which is aware of the locations. It stores a mapping such as:

mapping = {
        "temperature": ("main", "temp"),
        "wind_speed": ("wind", "speed"),
}

The .get() method refers to the mapping to get the right location. Then you can do:

let my_weather = Weather(weather_call.json())
let temperature = my_weather.get("temperature")

Additionally, you can replace .get() method with __getitem__() dunder method, which allows you to use square bracket syntax on your Weather class.

19

u/magus_minor Nov 02 '25 edited Nov 02 '25

Your json data is meant to be read by code, not a human. You can use the pprint module from the standard library to format the data to make it more readable by a human. Here's some code:

import pprint

# your json data
data = {"coord": {"lon": 139.6917, "lat": 35.6895}, "weather": [{"id": 804, "main": "Clouds", "description": "overcast clouds", "icon": "04d"}], "base": "stations", "main": {"temp": 18.68, "feels_like": 18.17, "temp_min": 17.03, "temp_max": 19.33, "pressure": 1012, "humidity": 60, "sea_level": 1012, "grnd_level": 1010}, "visibility": 10000, "wind": {"speed": 2.72, "deg": 62, "gust": 2.56}, "clouds": {"all": 100}, "dt": 1762049602, "sys": {"type": 2, "id": 268395, "country": "JP", "sunrise": 1762031030, "sunset": 1762069540}, "timezone": 32400, "id": 1850144, "name": "Tokyo", "cod": 200}

pprint.pprint(data)

When run it prints this:

{'base': 'stations',
 'clouds': {'all': 100},
 'cod': 200,
 'coord': {'lat': 35.6895, 'lon': 139.6917},
 'dt': 1762049602,
 'id': 1850144,
 'main': {'feels_like': 18.17,
          'grnd_level': 1010,
          'humidity': 60,
          'pressure': 1012,
          'sea_level': 1012,
          'temp': 18.68,
          'temp_max': 19.33,
          'temp_min': 17.03},
 'name': 'Tokyo',
 'sys': {'country': 'JP',
         'id': 268395,
         'sunrise': 1762031030,
         'sunset': 1762069540,
         'type': 2},
 'timezone': 32400,
 'visibility': 10000,
 'weather': [{'description': 'overcast clouds',
              'icon': '04d',
              'id': 804,
              'main': 'Clouds'}],
 'wind': {'deg': 62, 'gust': 2.56, 'speed': 2.72}}

which is much more readable than the original making it easier to figure out how to access bits of data.

If you want to access lots of fields in the "main" sub-dictionary do this:

main = data["main"]
print(main["temp"])
print(main["temp_max"])
# etc

17

u/Masterous112 Nov 02 '25

Is the json always in this format? If so then you can just do dictionary["main"]["temp"]

5

u/ParallelProcrastinat Nov 02 '25

Not really clear what issue you're having, but here's how I'd do it:

Parse the response with json.loads() and index into it using repeated [] operator as required.

If you want to get a pretty-printed json just to get a clearer idea of what it looks like, json.dumps() can do that with indent=2 (or 4 or whatever number of indent spaces you prefer).

5

u/storage_admin Nov 02 '25

Suppose your json data is assigned to a variable named data

You could access the temperature value as data['main']['temp']

For formatting the json you could use

import json
print( json.dumps(data, indent=4) )

Or paste the json into numerous online formatters or use a command line tool like jq to format the json so you can see the structure.

4

u/JollyUnder Nov 02 '25 edited Nov 02 '25

If the nested dict is unorganized you can use this function I wrote that checks for specified keys in a nested dictionary:

from collections.abc import Iterator, Iterable, Hashable
from typing import Any


def get_values_from_nested_dict(dictionary: dict, keys: tuple[Hashable, ...]) -> Iterator[Any]:
    if isinstance(dictionary, Iterable) and not isinstance(dictionary, str):
        if isinstance(dictionary, dict):
            for key in keys:
                if key in dictionary:
                    yield dictionary[key]
            dictionary = dictionary.values()
        for elem in dictionary:
            yield from get_values_from_nested_dict(elem, keys)


if __name__ == '__main__':
    data = {
        "base": "stations",
        "clouds": {
            "all": 100
        },
        "cod": 200,
        "coord": {
            "lat": 35.6895,
            "lon": 139.6917
        },
        "dt": 1762049602,
        "id": 1850144,
        "main": {
            "feels_like": 18.17,
            "grnd_level": 1010,
            "humidity": 60,
            "pressure": 1012,
            "sea_level": 1012,
            "temp": 18.68,
            "temp_max": 19.33,
            "temp_min": 17.03
        },
        "name": "Tokyo",
        "sys": {
            "country": "JP",
            "id": 268395,
            "sunrise": 1762031030,
            "sunset": 1762069540,
            "type": 2
        },
        "timezone": 32400,
        "visibility": 10000,
        "weather": [
            {
                "description": "overcast clouds",
                "icon": "04d",
                "id": 804,
                "main": "Clouds"
            }
        ],
        "wind": {
            "deg": 62,
            "gust": 2.56,
            "speed": 2.72
        }
    }

    keys = 'temp', 'description'
    values = get_values_from_nested_dict(data, keys)
    for value in values:
        print(value)

Output:

18.68
overcast clouds

3

u/odaiwai Nov 02 '25

Because the returned JSON is very badly formatted

It's not. it's formatted to be read in by something that understands JSON. In Python it's just a dict of data (some dicts, a list with a dict, some values), so you can parse it with a little loop:

data = get_json_from_api()
for key, value in data.items():
    print(key, value)
    if isinstance(value, dict):
        for key2, value2 in value.items():
            print('\t', key2, value2)

or just go for specific items directly with data['main']['temp'] as someone else suggested.

2

u/koldakov Nov 02 '25

I start always with the data structure

Define the model you need in pydantic/dataclasses, it’s much easier to work with that

1

u/TheRNGuy Nov 02 '25

Create a function for that. 

2

u/pachura3 Nov 02 '25

A recursive one!

1

u/TheRNGuy Nov 02 '25

No need, just hard-code it. 

1

u/Round_Ad8947 Nov 02 '25

If your data source is standardized, and you know how you want to work with the data, why not create an Observation class that you setup to access the values.

Bonus, you can write str(self) to roll up your print statements.

1

u/shisnotbash Nov 02 '25

For printing look at json.dumps(mydict, indent=2). Printing that statement will give you the prettiest JSON output. As for digging through the JSON itself, you may want to create a class that takes that un marshaled JSON as kwargs to the initializer. Then you can set attributes or getters that are more friendly than having to dig through the keys constantly. You may even want some of your keys inside the JSON to be their own classes. Python is nice in that it makes it possible to operate on arbitrary data like this without having to clearly define it, but (as you can see) it can also make things kinda messy. If you don’t want to cast your JSON to a class then an alternative is to make “getter” functions to search and return specific elements nested in your JSON.

1

u/shisnotbash Nov 02 '25

Also, if you just want pretty JSON output including color, then you can print the JSON (the actual JSON string and not the dict) and pipe it to jq in your terminal.

1

u/hulleyrob Nov 02 '25

I can recommend yq for when you find someone’s json has a space in it and jq won’t pretty print it. Just thought I’d share.

1

u/LongjumpingWinner250 Nov 02 '25

Could use some sort of breadth search

1

u/neums08 Nov 02 '25

print(json.dumps(dict_data, indent=2))

1

u/jimtk IdontChat Nov 02 '25

Since you are already deep in json use json!

import json

data = {"coord": {"lon": 139.6917, "lat": 35.6895}, "weather": [{"id": 804, "main": "Clouds", "description": "overcast clouds", "icon": "04d"}], "base": "stations", "main": {"temp": 18.68, "feels_like": 18.17, "temp_min": 17.03, "temp_max": 19.33, "pressure": 1012, "humidity": 60, "sea_level": 1012, "grnd_level": 1010}, "visibility": 10000, "wind": {"speed": 2.72, "deg": 62, "gust": 2.56}, "clouds": {"all": 100}, "dt": 1762049602, "sys": {"type": 2, "id": 268395, "country": "JP", "sunrise": 1762031030, "sunset": 1762069540}, "timezone": 32400, "id": 1850144, "name": "Tokyo", "cod": 200}
text = json.dumps(data, indent=4)
print(text)

Ouput

{
    "coord": {
        "lon": 139.6917,
        "lat": 35.6895
    },
    "weather": [
        {
            "id": 804,
            "main": "Clouds",
            "description": "overcast clouds",
            "icon": "04d"
        }
    ],
    "base": "stations",
    "main": {
        "temp": 18.68,
        "feels_like": 18.17,
        "temp_min": 17.03,
        "temp_max": 19.33,
        "pressure": 1012,
        "humidity": 60,
        "sea_level": 1012,
        "grnd_level": 1010
    },
    "visibility": 10000,
    "wind": {
        "speed": 2.72,
        "deg": 62,
        "gust": 2.56
    },
    "clouds": {
        "all": 100
    },
    "dt": 1762049602,
    "sys": {
        "type": 2,
        "id": 268395,
        "country": "JP",
        "sunrise": 1762031030,
        "sunset": 1762069540
    },
    "timezone": 32400,
    "id": 1850144,
    "name": "Tokyo",
    "cod": 200
}

1

u/magus_minor Nov 03 '25

An easier way to just get the value without having it feel like sifting through a haystack?

If you mean not having to understand the structure of the data, then no, you have to know where the data you want is.

There is a way to simplify getting the required data. Instead of getting the temperature by doing data["main"]["temp"] you can restructure the data so getting the temperature becomes data.main.temp. This code converts the nested dictionary data from the JSON into a nested set of namedtuples which lets you do the attribute lookup. You still need to understand the structure of the data.

from collections import namedtuple

def dict2ntuple(d):
    """Return namedtuple given a dictionary.
    
    Recursively converts all sub-dictionaries.
    """
    
    nt = namedtuple('result', d)
    data = []
    for (key, value) in d.items():
        if isinstance(value, dict):
            value = dict2ntuple(value)
        data.append(value)
    return nt(*data)
    
data = {"coord": {"lon": 139.6917, "lat": 35.6895}, "weather": [{"id": 804, "main": "Clouds", "description": "overcast clouds", "icon": "04d"}], "base": "stations", "main": {"temp": 18.68, "feels_like": 18.17, "temp_min": 17.03, "temp_max": 19.33, "pressure": 1012, "humidity": 60, "sea_level": 1012, "grnd_level": 1010}, "visibility": 10000, "wind": {"speed": 2.72, "deg": 62, "gust": 2.56}, "clouds": {"all": 100}, "dt": 1762049602, "sys": {"type": 2, "id": 268395, "country": "JP", "sunrise": 1762031030, "sunset": 1762069540}, "timezone": 32400, "id": 1850144, "name": "Tokyo", "cod": 200}
    
nt = dict2ntuple(data)
main = nt.main
print(f"{main.temp=}")
print(f"{main.temp_min=}")
clouds = nt.clouds
print(f"{clouds.all=}")
print(f"{nt.sys.country=}")

It really isn't clear that this is worth doing. It's simpler to write the code but you only do that once and you have to convert new data to the namedtuple form every time you read it. An added complication is that named tuples can't handle certain key names, though that's not a problem with your data.

1

u/white_nerdy Nov 04 '25 edited Nov 04 '25

If your JSON is in a variable called d, you can simply do d.keys() to see available keys. E.g. d["main"].keys() or d["weather"][0].keys().

You can use the indent parameter of dumps to put the same JSON in a multiline format with indentation. Here is a list of all the available parameters for dump / dumps. Usually I prefer indent=1, it looks like this:

>>> d = (paste your data here)
>>> import json
>>> print(json.dumps(d, indent=1))
{
 "coord": {
  "lon": 139.6917,
  "lat": 35.6895
 },
 "weather": [
  {
   "id": 804,
   "main": "Clouds",
   "description": "overcast clouds",
   "icon": "04d"
  }
 ],
 "base": "stations",
 "main": {
  "temp": 18.68,
  "feels_like": 18.17,
  "temp_min": 17.03,
  "temp_max": 19.33,
  "pressure": 1012,
  "humidity": 60,
  "sea_level": 1012,
  "grnd_level": 1010
 },
 "visibility": 10000,
 "wind": {
  "speed": 2.72,
  "deg": 62,
  "gust": 2.56
 },
 "clouds": {
  "all": 100
 },
 "dt": 1762049602,
 "sys": {
  "type": 2,
  "id": 268395,
  "country": "JP",
  "sunrise": 1762031030,
  "sunset": 1762069540
 },
 "timezone": 32400,
 "id": 1850144,
 "name": "Tokyo",
 "cod": 200
}