I am getting well when I work with the JSON format data from dictionary object (associative array) using json module(import json) .

When you use “append” in pyhon to dictionary objects(associative array),you will be getting “KeyError” if you access with a key that does not exist in the dictionary. If it happens, you can fix using the “get()” function.

You will use the “extend” rather than “append” when you unpleasant comes to nest list(multiple list). But if you use the “get()” because of the key that does not exist in the dictionary,then will return “None”.

“None” causes error “TypeError: ‘NoneType’ object is not iterable”. So, Solution is to use the “try-except-else statement”. You just write “pass” if you do not want to do anything at the part of the except-else.

# contents of the data are the same as examples of "append"
for i in data['menuitem']:
try:
datalistB.extend(i.get('value')) # ['New', 'Open', 'Create', 'Read']
except TypeError:
print "Someting" # If not "try-except-else" statement, "TypeError: 'NoneType' object is not iterable"
else:
pass

Please Share

Please share if you feel this article is useful for someone.

https://www.amelt.net/en/%e6%9c%aa%e5%88%86%e9%a1%9e-en/3647/pyhon:Solution of append “KeyError”,extend “TypeError: ‘NoneType’ object is not iterable”