How to get a JSON response using ChatGPT

How to get a JSON response using ChatGPT

OpenAI released ChatGPT in November 2022. It's only been just a few months and it seems like to be getting smarter every day. The question is how can we leverage this as Software Engineers?

Since Software Engineers usually consume data in JSON format, in this article, I'll give tips on how to command ChatGPT to help us structure its response in JSON format.


Goal

Our goal is to ask ChatGPT to give us a question for an interview. So, let's first try the basic prompt.

Basic Prompt

Prompt:

Result:

Not bad. Let's take it up a step, shall we?

Let's ask ChatGPT if it could give us an answer in a JSON format.

Improved JSON Format

Prompt:

Gave us a result of:

ChatGPT is smart enough to recognize and format it to our desired outcome.

Here's where the problem arises. Since we gave it a very general request:

Your response should be in JSON format

ChatGPT will sometimes go its way to give you different keys.

This will be a problem when you start consuming the data on the front end.

So, how can we fix this?

Tips for a consistent JSON format

Here are some few tips to get your desired outcome every time. The key is to be very specific with your request.

  • Use 'Your response should be in JSON format'.
ex: "Generate a question for an interview. Your response should be in JSON format."
  • Use the keyword "object" to indicate the {} bracket
ex: Generate a question for an interview. Your response should be in JSON format. Create an unnamed object.
  • Use the keyword "parameters" to indicate the key
ex: Generate a question for an interview. Your response should be in JSON format. Create an unnamed object, and add a parameter 'question' containing the result.

Combine it together and use it as a prompt:

Result 🎉:

Advanced Improvements

Now that we can command ChatGPT to give us the specific structure of our JSON format, let's try asking it to give us a list of questions.

#1 Goal: tell ChatGPT that we want a JSON format. Let's write the following:

Generate a question for an interview. Your response should be in JSON format.

#2 Goal: get a list of objects. Let's add the following:

Generate a question for an interview. Your response should be in JSON format. Create a list object with the parameter 'questions'.

#3 Goal: create an object inside the list. Let's add the following:

Generate a question for an interview. Your response should be in JSON format. Create a list object with the parameter 'questions'. Inside the list are question objects with a parameter 'difficultyLevel' containing the difficulty level and a parameter 'question' containing the question.

Use it as a prompt:

Result:

And voila 🎉

Conclusion

Writing prompts aren't exactly science. It's an art.

When trying to get the desired outcome it will be a trial-and-error process. The good thing is, ChatGPT is still free as of this writing.

This gives you the opportunity to practice your prompts before directly using them in the OpenAI APIs which could incur costs if you're not mindful.