Skip to content
Advertisement

Python pass array into function and use it in json return

I have the following python code:

JavaScript

I wanted to put a try/except on the requestAPI function so it does’nt break the code. I thought about this:

JavaScript

I could do something like:

JavaScript

But i think there’s a better way of doing this.

Advertisement

Answer

You can achieve it without a try-except via dict.get():

JavaScript

This will return the value for key key if it exists in the JSON otherwise it will return None. If you want it to return False, do .get(key, False).

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement