r/learnpython • u/Yelebear • 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
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"])
# etc17
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 clouds3
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
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
1
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
}
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
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.