I'm dealing with JSON data which I load into Python dictionaries. A lot of these have optional fields, which then may contain dictionaries, that kind of stuff.
dictionary1 =
{"required": {"value1": "one", "value2": "two"},
"optional": {"value1": "one"}}
dictionary2 =
{"required": {"value1": "one", "value2": "two"}}
If I do this,
dictionary1.get("required").get("value1")
this works, obviously, because the field "required"
is always present.
However, when I use the same line on dictionary2
(to get the optional field), this will produce an AttributeError
dictionary2.get("optional").get("value1")
AttributeError: 'NoneType' object has no attribute 'get'
which makes sense, because the first .get()
will return None
, and the second .get()
cannot call .get()
on the None object.
I can solve this by giving default values in case the optional field is missing, but this will be annoying the more complex the data gets, so I'm calling this a "naive fix":
dictionary2.get("optional", {}).get("value1", " ")
So the first .get()
will return an empty dictionary {}
, on which the second .get()
can be called, and since it obviously contains nothing, it will return the empty string, as defined per the second default.
This will no longer produce errors, but I was wondering if there is a better solution for this - especially for more complex cases (value1
containing an array or another dictionary, etc....)
I could also fix this with try - except AttributeError
, but this is not my preferred way either.
try:
value1 = dictionary2.get("optional").get("value1")
except AttributeError:
value1 = " "
I also don't like checking if optional field exists, this produces garbage code lines like
optional = dictionary2.get("optional")
if optional:
value1 = optional.get("value1")
else:
value1 = " "
which seems very non-Pythonic...
I was thinking maybe my approach of just chaining .get()
s is wrong in the first place?