Error when a key doesn’t exist in json you’re parsing with python
You might be parsing by means of json with python and making an attempt to print out values like this:
jsonfile="information.json"
jf = open(jsonfile, "r")
j=json.load(jf)gadgets=j['items']for merchandise in gadgets:
v1=merchandise['name1']
v2=merchandise['name2']
One downside that causes this error is when an merchandise in your checklist of things doesn’t have values for each name1 and name2.
In that case (and it’s a good suggestion anyway), you’ll wish to examine if the identify: worth pair exists previous to making an attempt to acquire the worth.
jsonfile="information.json"
jf = open(jsonfile, "r")
j=json.load(jf)gadgets=j['items']for merchandise in gadgets: if v1 in merchandise:
v1=merchandise['name1'] if v2 in merchandise:
v2=merchandise['name2']
After all you’ll wish to think about using some abstraction right here to get rid of the repetitive code as defined in these two posts:
Teri Radichel
For those who favored this story please clap and observe:
Medium: Teri Radichel or E-mail Checklist: Teri Radichel
Twitter: @teriradichel or @2ndSightLab
Requests providers through LinkedIn: Teri Radichel or IANS Analysis
© 2nd Sight Lab 2022
____________________________________________
Writer:
Cybersecurity for Executives within the Age of Cloud on Amazon
Want Cloud Safety Coaching? 2nd Sight Lab Cloud Safety Coaching
Is your cloud safe? Rent 2nd Sight Lab for a penetration check or safety evaluation.
Have a Cybersecurity or Cloud Safety Query? Ask Teri Radichel by scheduling a name with IANS Analysis.
Cybersecurity & Cloud Safety Sources by Teri Radichel: Cybersecurity and Cloud safety lessons, articles, white papers, displays, and podcasts