Make a basic chat-bot with a JSON file using Python.

Make a basic chat-bot with a JSON file using Python.

Hello and welcome to today's tutorial on how to make a chatbot in Python! Chatbots have become increasingly popular in recent years, and for good reason. They can provide users with instant responses to their questions and can be programmed to handle a wide variety of tasks. In this tutorial, we will walk you through the process of creating a chatbot using Python. We'll cover everything from setting up the necessary libraries and tools to building a basic chatbot that can respond to user input. By the end of this tutorial, you'll have a solid foundation in building chatbots with Python and will be able to continue exploring this exciting field on your own. So let's get started!

Create JSON File

First, you need to have a JSON file that contains the questions and answers for your chatbot. Here's an example of what your JSON file could look like:

    "greetings": [
        "Hi there!",
    "questions": [
            "question": "What is your name?",
            "answer": "My name is Chatbot."
            "question": "How old are you?",
            "answer": "I am a computer program, so I don't have an age."
            "question": "What can you do?",
            "answer": "I can answer your questions and have a conversation with you."
    "farewells": [
        "See you later!",
        "Take care!"

In this example, the JSON file has three sections: greetings, questions, and farewells. Each section contains an array of possible responses. The questions section also contains an array of objects, where each object contains a question and its corresponding answer.

Create python file

To create the chatbot, you can use the json module in Python to read the JSON file and store its contents in a dictionary. Here's some sample code:

import json
import random

# Load the JSON file
with open('chatbot.json', 'r') as f:
    chatbot_data = json.load(f)

# Define the chatbot function
def chatbot():

    while True:
        user_input = input("You: ")

        if user_input.lower() in ['exit', 'bye', 'goodbye']:

        for question in chatbot_data['questions']:
            if user_input.lower() == question['question'].lower():
            print("I'm sorry, I don't understand.")

In this example, the chatbot() function uses the random module to choose a random greeting and farewell from the chatbot_data dictionary. It then enters a while loop to continuously ask the user for input.

If the user enters "exit", "bye", or "goodbye", the chatbot will choose a random farewell and exit the loop. Otherwise, the chatbot will loop through the questions array to see if the user's input matches any of the questions. If it does, the chatbot will print the corresponding answer. If not, the chatbot will say it doesn't understand.

You can call the chatbot() function to start the chatbot. Note that you'll need to replace chatbot.json with the name of your JSON file.

Good luck with making your own. If you have any Queries or Suggestions, please reach out to me in the Comments Section below.

Did you find this article valuable?

Support Amit Gajare by becoming a sponsor. Any amount is appreciated!