In this article we'll go over different ways to save time while programming by using ChatGPT's hability to generate code.

In the world of software development, time is a precious commodity. Developers are constantly seeking ways to automate repetitive tasks in order to maximize their efficiency. With the arrival of advanced technologies such as artificial intelligence and machine learning, the field of software development has seen a significant shift in recent years. One that has the potential to revolutionize the way developers work is ChatGPT, a large language model developed by OpenAI.

From generating data for applications to creating SQL scripts to populate tables, ChatGPT can help developers optimize their workflow and increase their productivity. In this article, we will explore how ChatGPT can save developers time and effort through code generation. 

Creating scripts with ChatGPT

Have you ever needed to build (or wish you had at hand) a script to accomplish a simple but extensive task? Like getting data from a file and saving it in a specific format? While we may be able to implement it, we will most likely take way longer than ChatGPT. So why don't we delegate this task to AI?

Below is an example of a script that reads an Excel file and returns the data in JSON format.

Question: 

Generate a code in Python that reads an Excel file and returns a JSON file with its content

Response: 

Sure, here's an example code in Python that uses the pandas library to read an Excel file and convert it to a JSON file:

In this example, we first import the pandas library, which provides tools for data analysis in Python. We then use the read_excel() function to read an Excel file called "example.xlsx" and store the data in a pandas DataFrame object called df.

Next, we use the to_json() function to convert the data to a JSON object. The orient='records' argument specifies that we want the JSON to be formatted as a list of records, where each record represents a row in the original Excel file.

Finally, we write the JSON data to a file called "example.json" using Python's built-in open() function and the write() method.

In this example, we have a simple code in Python that does what we requested, where the columns of the table will be the properties of the JSON object. From this example, we see that we can easily accomplish a task by using ChatGPT to create a script for us. We can also note that it returns an explanation of the generated code as well.

Generating classes from an object

Question: 

Generate a class in Java for the following object:

Response:

As we can see, it created the classes following the attributes of our JSON object. This can be used when we know the contract for a request (for example, when it's exposed by Swagger) and we want to create a structure that represents that to add it to our code. We can also note that it already created getters and setters for our class, which also saves us some time.

Don't worry about date formatting anymore

One thing that demands time while programming is dealing with dates. They are handled differently in each programming language, and sometimes it can be very confusing. Instead of losing a lot of time researching and understanding the best way to transform a date object into the format you want, we can try asking ChatGPT to do it for us.

Question: 

Create a code in javascript that receives an ISO date and converts it into a date in the following format: dd/mm/yyyy

Response:

Sure, here's a simple code in JavaScript that receives an ISO date string and converts it to a date string in the format of dd/mm/yyyy:

To use this function, simply pass an ISO date string as an argument:

The function creates a new Date object from the ISO date string and then extracts the day, month, and year components from it using various methods available in the Date object. The padStart function is used to ensure that the day and month components are always two digits long. Finally, the function returns a string in the desired format of dd/mm/yyyy.

Generate data to use in your applications

Sometimes applications need to use publicly available data, like a list of cities in a state or a list of all countries around the world. You might find that information wrapped into some data structure online, which you can either use as it is or adapt to another structure you prefer, but there is a more practical way to do that. You can ask ChatGPT to generate that same data in any format you'd like.

Question: 

List in JSON format the New Seven Wonders of the World with some propertiesAnswer:Sure, here's an example of a JSON object representing the New Seven Wonders of the World with some of their properties:

Response:

Sure, here's an example of a JSON object representing the New Seven Wonders of the World with some of their properties:

This JSON object has an array called "wonders", which contains seven objects, each representing one of the New Seven Wonders of the World. Each wonder object has properties such as "name", "location", "year_built", and "description". These properties provide information about each wonder, such as their name, where they are located, when they were built, and what they were.

Generate SQL scripts to populate your tables

Extending the idea presented in the last topic about generating data, you can populate tables in your application using AI. To do so, you can ask ChatGPT to generate SQL scripts that you can run in your database manager.

Question:

Generate an SQL script to populate a table called "wonders" with the last answer

Response: 

Sure! Here's an SQL script that would create a "wonders" table and insert the data from the JSON object representing the New Seven Wonders of the World:

This SQL script creates a table called "wonders" with five columns: "id" (an auto-incrementing primary key), "name", "location", "year_built", and "description". It then inserts the data from the JSON object representing the New Seven Wonders of the World into this table using a series of INSERT INTO statements.

Generate rest API to communicate with the database

We can generate a REST API from the last response, just ask ChatGPT to generate it from the SQL script.

Question:

Create a REST API for this SQL table:

Response:

Sure, here is an example of a REST API for the "wonders" SQL table:

Set up the database connection:

Before you can create a REST API, you need to establish a connection to your MySQL database. Here's an example of how you can do it using the "mysql-connector-python" package:

Replace "yourusername", "yourpassword", and "yourdatabase" with your own MySQL credentials.

Create the REST API endpoints:

Next, you need to create the endpoints for your REST API. Here's an example of how you can do it using the Flask framework:

Test the REST API:

You can now test your REST API using a tool like Postman. Here are some example requests you can make:

That's it!

Generate a collection to test the REST API

After generating the REST API, we need to test it. We can ask ChatGPT to give us a postman collection for each API request.

Question:

Generate a CURL to import in Postman to test a CRUD with the following properties:

Response:

Here is an example of a CURL command to test a CRUD API for the given wonders table:

GET all wonders:

GET a single wonder:

POST a new wonder:

PUT (update) a wonder:

DELETE a wonder:

Note: Replace localhost:5000 with the base URL of the API, and 1 with the ID of the wonder you want to manipulate in the above CURL commands.

Generate a service in React to consume the REST API

We can generate a code to test the API too. Let's see an example with React for consuming the Rest API.

Question:

Generate a service in React to consume a Rest API with the following properties:

Response:

To generate a React service to consume a REST API with the wonders table, you can follow these steps:

  1. Create a new React project using npx create-react-app my-app (replace my-app with your preferred project name).
  2. Install the axios library using npm install axios.
  3. Create a new file called api.js in your src folder. This file will contain functions for making API requests.
  4. Import axios into api.js.
  5. Create a base URL for your API by defining a constant variable BASE_URL to the root URL of your API.
  6. Define a function to get all wonders from the database. This function should make a GET request to /wonders and return the response data.
  7. Define a function to get a single wonder by ID. This function should take an id parameter, make a GET request to /wonders/${id}, and return the response data.
  8. Define a function to add a new wonder to the database. This function should take a wonder parameter, make a POST request to /wonders, and return the response data.
  9. Define a function to update an existing wonder in the database. This function should take an id parameter and a wonder parameter, make a PUT request to /wonders/${id}, and return the response data.
  10. Define a function to delete a wonder from the database. This function should take an id parameter, make a DELETE request to /wonders/${id}, and return the response data.

Here's an example implementation of api.js:

You can then import and use these functions in your React components as needed. For example, here's a component that displays a list of wonders:

Here we see that ChatGPT generated a React application with a service that consumes the API and a component to display the data. You can add the generated code to your project and adapt it to fit your needs.

Conclusion

In this article we went over some tasks that ChatGPT can accomplish when it comes to code generation. With the ability to quickly generate scripts, classes and SQL statements, it can be used by developers to save time and effort while coding. As a result, we can think of ChatGPT as a valuable asset to those who seek to automate repetitive tasks, optimize their work and increase productivity.

Authors

Guilherme Coelho

Thais Morato