ChatGPT and JSON Responses: Prompting & Modifying Code-Friendly Objects
A Guide to Building and Updating JSON Objects Using Simple Conversations
As I’ve delved deeper into ChatGPT, I often contemplate ways to integrate its response with code in an application. Receiving responses in JSON-formatted code blocks is preferable for software compared to the standard block of text, which is ChatGPT’s default.
Moreover, it’s crucial to be able to modify these objects within an application. here, I’ll demonstrate how to:
Create a JSON object and assign it a reference name.
Update values using conversational language.
Modify using arrays and objects.
Auto-Generate based on the object as a template.
NOTE: The script and responses below are derived from https://chat.openai.com/4.0.
Creating a JSON Object Response: One method that harmonizes well with ChatGPT is assigning a reference name to “the object.” In this case, since my object represents a character, I’ve chosen a concise nickname, “bob”, to refer to in subsequent prompts.
PROMPT: “Create a JSON object with the fields ‘name’ and ‘age’. Let’s name this object Bob. Assign ‘Bob Main’ as the name value and ‘38’ as the age value.”
RESPONSE:
{
"name": "Bob Main",
"age": 38
}
Updating Values:
Programmatically, one might desire a very specific modification command, such as “Add 1 to the ‘age’ value of the ‘Bob’ object.” Yet, for the sake of this demonstration, we can phrase it more colloquially.
PROMPT: “Have Bob age by one year.”
RESPONSE:
{
"name": "Bob Main",
"age": 39
}
Just like that, Bob’s age increments from 38 to 39. It’s worth noting that I’ve only displayed the JSON response here. ChatGPT also shares its thought process in these responses. Refer to the attached screenshot for the comprehensive answer.
Creating and Modifying Arrays
PROMPT: “Add the field “lucky_numbers” with a value of an array with 4 indexes of 7,11,16,8.”
RESPONSE:
{
"name": "Bob Main",
"age": 39,
"lucky_numbers": [7, 11, 16, 8]
}
You can also use more programmatic terms like…
PROMPT: “Prepend 42 to lucky numbers and append 111.”
RESPONSE:
{
"name": "Bob Main",
"age": 39,
"lucky_number": [42, 7, 11, 16, 8, 111]
}
Creating and Modifying Nested Objects with Random Values
Diving into more intricate tasks, let’s attempt to create a nested object with randomly generated values.
PROMPT: “Add the field ‘location’ with an object value containing fields ‘x’ and ‘y’. Assign both fields a random number between 1 and 100.”
RESPONSE:
{
"name": "Bob Main",
"age": 39,
"lucky_number": [42, 7, 11, 16, 8, 111],
"location": {
"x": 56,
"y": 87
}
}
Ok, now it’s getting fun. I now wanna see how well ChatGPT can infer what this object could be used for and modify it based on a more vague and abstract prompt.
PROMPT: “Move bob two spaces back.”
Here’s the full response as a screenshot where the full response is pretty interesting. While ChatGPT acknowledges the ambiguity of the instruction, it still manages to make a reasonable assumption — especially considering “back” is open to interpretation in this context.
Auto-Generating New Objects
So, I was pretty explicit as I built this object out. But let’s see what happens when we try to create new objects using this as an example.
PROMPT: “Create two new objects called ‘player_1’ and ‘player_2’. They should have the same fields as ‘Bob’. However, come up with new and unique values for both of the two new objects.”
RESPONSE:
{
"player_1": {
"name": "Alice Green",
"age": 29,
"lucky_number": [13, 21, 3, 45, 54],
"location": {
"x": 67,
"y": 32
}
},
"player_2": {
"name": "Charlie Brown",
"age": 45,
"lucky_number": [1, 9, 77, 23, 50],
"location": {
"x": 89,
"y": 12
}
}
}
🤯
As I continue to tinker with ChatGPT, I’m finding that JSON objects are a really valuable way to keep track of and modify data. And, you can still be very conversational. ChatGPT is less likely to change other values without being explicitly asked. And, it’s kind of a good way to keep “us” more on track with what we’re asking as well.
Cheers 🍻,
- Bob