Official Jokes Database

Official Joke API is a great source for fun and creative jokes. Here is a simply code that uses requests, json and webbrowser libraries:

import requests
import json


def jokes(f):
    
    data = requests.get(f)
    tt = json.loads(data.text)
    return tt

f = r"https://official-joke-api.appspot.com/random_ten"
a = jokes(f)

for i in (a):
    print(i["type"])
    print(i["setup"])
    print(i["punchline"], "\n")
  • general
    What did Michael Jackson name his denim store?
    Billy Jeans!
  • general
    Who did the wizard marry?
    His ghoul-friend
  • general
    What happens to a frog’s car when it breaks down?
    It gets toad away
  • general
    What’s blue and not very heavy?
    Light blue.
  • general
    Did you hear that David lost his ID in prague?
    Now we just have to call him Dav.
  • programming
    Why do Java programmers wear glasses?
    Because they don’t C#
  • general
    How did Darth Vader know what Luke was getting for Christmas?
    He felt his presents.
  • general
    What do you call a dad that has fallen through the ice?
    A Popsicle.
  • general
    How do you make holy water?
    You boil the hell out of it.
  • general
    Which side of the chicken has more feathers?
    The outside.

Here is the code for 1 random joke only:

import requests
import json


def jokes(f):
    
    data = requests.get(f)
    tt = json.loads(data.text)
    return tt

f = r"https://official-joke-api.appspot.com/random_joke"
a = jokes(f)

for i in (a):
    print(i["type"])
    print(i["setup"])
    print(i["punchline"], "\n")

general
What’s the best thing about elevator jokes?
They work on so many levels.

Finally, this api also works with joke categories, here are some of the categories you can try:

  • general
  • programming
  • knock-knock
import requests
import json


def jokes(f):
    
    data = requests.get(f)
    tt = json.loads(data.text)
    return tt

f = r"https://official-joke-api.appspot.com/jokes/programming/random"
a = jokes(f)

for i in (a):
    print(i["type"])
    print(i["setup"])
    print(i["punchline"], "\n")

programming
A SQL query walks into a bar, walks up to two tables and asks…
‘Can I join you?’

Source of API: Official Joke API

If you need a refresher on user defined functions lesson in Python here is a link to our Defining Functions Lesson.

Recommended Posts