Skip to content
Advertisement

How to check for what JSON data exists in a Python HTTP Request?

I’m working on a script with Python that will generate a joke using an API I found online. (https://sv443.net/jokeapi/v2/). However, some of the setup/question parts of the joke use JSON Data, which varies between being ‘setup’ and ‘joke’. I’m looking to see if I can write a script that will check which one the response is pulling. I have my script here:

JavaScript

Some of the responses are

JavaScript

and

JavaScript

Is there a way to check which data-name the response is getting?

Advertisement

Answer

There are two types of jokes. Single liners and multiple part ones.

Single line joke:

JavaScript

Multiple part joke:

JavaScript

You need to check the joke type prior to querying the other json elements.

JavaScript
Advertisement