Understanding API and JSON in Vonage AI Studio with Burger Examples
Published on February 12, 2024

Grilling the Basics

In this blog, we will simplify the concepts of JSON objects, API responses, and response mapping in Vonage AI Studio. We'll cover these topics with straightforward, relatable burger examples (inspired by Bob's Burgers). Whether you're a seasoned developer or new to API integrations, this guide will provide valuable insights into making the most of Vonage AI Studio's capabilities. As Linda Belcher would say: "The real tragedy is that I don't have time to get nachos before we start." Let's dig in!

How to Use Webhooks with Vonage AI Studio

Webhooks are vital in modern web applications, serving as a channel for real-time data transmission. In Vonage AI Studio, the webhooks node plays a pivotal role, enabling interactions with external services and APIs. In AI Studio, webhooks act as listeners for events from external services, triggering predefined actions within your workflows. When a webhook receives data, the response mapping feature allows you to extract specific information from the response and use it in subsequent steps of your workflow. This capability is crucial for creating dynamic and responsive applications.

Tricks and Tools for Locating JSON Objects

Understanding and navigating through JSON (JavaScript Object Notation) responses is a critical skill when working with REST APIs, including those from Vonage. As I mentioned earlier, I enjoy working with JSON because of its readability and simplicity. However, when dealing with complex or large JSON structures, finding the exact data you need can be challenging. Thankfully, there are user-friendly tools that make locating JSON objects straightforward, even for those new to working with API responses.

What is a JSON Object?

First, let’s understand what a JSON object is. In its simplest form, a JSON object is a collection of key-value pairs enclosed in curly braces {}. Each key is a unique identifier (usually a string), and the value associated with it can be a number, string, boolean, another object, an array, or null. For example:

{
  "item": "burger",
  "quantity": 2,
  "withFries": true,
  "extras": ["cheese", "lettuce"],
  "specialInstructions": null
}

In this JSON object:

  • item (Text): The item ordered is a "burger".

  • quantity (Number): The quantity is represented as a number, 2.

  • withFries (Boolean): This key indicates whether fries are included with the order (true or false). Here, it's true.

  • extras (Array): Lists the additional toppings as an array, including "cheese" and "lettuce".

  • specialInstructions (Null): Used for any additional instructions, currently has no value (null).

This example provides a clear and simple representation of a burger order, utilizing different types of values in JSON format.

In this example, item, quantity, withFries, extras, and specialInstructions are keys, and "burger", 2, true, ["cheese", "lettuce"], and null are their respective values.

How to Navigate JSON Responses

  1. Understanding JSON Structure: Start by familiarizing yourself with the structure of the JSON response. Identify the hierarchy of objects and arrays (lists of values).

  2. Formatting for Clarity: JSON responses from APIs can sometimes be compact and hard to read. Use a formatter tool to 'prettify' the JSON, making it more readable. Tools like JSONLint (jsonlint.com) provide this functionality. They take a compact JSON string and format it with proper indentation and line breaks.

Tools for Locating JSON Objects

  1. JSONLint: As mentioned, JSONLint is great for formatting JSON. It also validates the JSON syntax, helping you catch and correct errors that might prevent proper parsing.

  2. JSONPath Finder: For more complex JSON structures, tools like JSONPath Finder can be invaluable. These tools allow you to query and extract specific parts of the JSON. You enter a 'path' using a query language similar to XPath for XML, and the tool returns the part of the JSON that matches your query.

Using the example above of orders, imagine we wanted to know how many orders we sold with Fries today. $.orders[*].withFries. Similarly, in a JSON response from Vonage that includes a list of call records, you might want to extract the caller ID from each record. With a JSONPath query like $.calls[*].callerID, you can quickly get all caller IDs from the list.

How to Understand REST API Responses

REST API stands for "Representational State Transfer Application Programming Interface."

  • Representational State Transfer (REST) is an architectural style for designing networked applications. It relies on a stateless, client-server communication protocol, almost always HTTP (Hypertext Transfer Protocol).

  • Application Programming Interface (API) is a set of rules and definitions that allows different software applications to communicate with each other.

In the context of web services, a REST API provides a way for applications to interact with web services using the protocols of the web (like HTTP) and typically involves requesting and transferring data using operations like GET, POST, PUT, and DELETE. REST APIs are widely used due to their simplicity, scalability, and versatility. That is what makes them the backbone of web communication, and understanding their responses is key to successful integration.

API responses often come in JSON format. As a visual person, I absolutely love how lightweight and readable the JSON structure is when it's made prettier. We'll explore the nuances of REST API responses, emphasizing the JSON format's role in ensuring efficient data exchange and integration with Vonage AI Studio.

Creating an Object Path

When working with JSON responses from APIs, especially in tools like Vonage AI Studio, one of the most crucial skills is understanding and creating "object paths."

What is an Object Path?

An object path is a sequence of keys that you follow to navigate through a JSON object to reach the specific data you want. Think of an object path as a GPS route that leads you to the exact piece of information you need within a JSON structure. For example, in a JSON object representing a person, the object path to find their email might look like person.contact.email.

How to Construct an Object Path

  1. Start from the Root: The root of a JSON object is its outermost level. From here, you identify the first key that leads you closer to your data.

  2. Navigate Through Layers: If the value of a key is another object or an array, you continue by specifying the next key or array index.

  3. Repeat Until You Reach Your Data: Continue specifying keys or indexes until you reach the data you need.

Practical Example in Vonage AI Studio

Let’s use a simple JSON response to illustrate how to construct an object path. Imagine you have the following JSON response from a Vonage API:

{
  "order": {
    "details": {
      "item": "burger",
      "size": "large",
      "customizations": {
        "extras": ["cheese", "lettuce", "tomato"],
        "exclude": ["pickles"],
        "sauces": ["ketchup", "mustard"]
      }
    },
    "customerInfo": {
      "name": "Bob",
      "contact": {
        "email": "bob@bobsburgers.com",
        "phone": "555-6789"
      }
    }
  }
}
  • Objective: Let's say we want to extract the excluded item(s) in the burger order.

  • Constructing the Extended Object Path:

    1. Start at the Root: The root key is order.

    2. First Layer: Inside order, we navigate to details.

    3. Second Layer: Within details, we find customizations.

    4. Final Destination: The customizations object contains the key exclude.

  • Final Object Path: The object path to find the list of sauces is order.details.customizations.exclude.

How to Use Object Paths in Vonage AI Studio

In Vonage AI Studio, when setting up response mappings or configuring nodes in the webhook node, you’ll often need to specify these object paths to tell the Studio exactly where to find and extract the data you need from an API response. Object paths are essential because they allow you to pinpoint and extract only the data you need from potentially complex JSON structures, making your data handling efficient and error-free.

How to Pass/Add Query Parameters to API Requests

In this section, we’ll explore how setting parameters in API interactions is similar to customizing your food order. Just like how you might specify how you want your burger at a restaurant, parameters in an API request let you define exactly what data you need. We'll use a straightforward analogy of ordering a burger to explain this concept, along with a sample data set to illustrate how it applies in a practical scenario.

Example: A Burger Order

Imagine you're at Bob's Burgers, and you want to order a burger. However, you don't like pickles. So, you tell the server, "I'd like a cheeseburger, but please hold the pickles." Here, 'no pickles' is a special request or parameter that customizes your order.

What Are Query Parameters?

  • Real-world Analogy: Query parameters are like your preferences in a food order. When you request something specific, like a burger without pickles, you're setting parameters for your order.

  • In API Terms: Parameters are the values you send with your API request. They dictate what information you want from the API. For example, if you're requesting information about a user, a parameter might be the user's ID. They let you customize your data request. Using parameters means you get exactly and only what you need from an API.

Translating to API Parameters

  • API Request: In the world of APIs, a request is like your order to the kitchen. The details you provide (like 'no pickles') are parameters that customize this request.

  • Setting Parameters: When you specify certain details in your API request, you're setting parameters. These parameters tell the API exactly how to 'prepare' your data.

Setting Parameters in Vonage AI Studio

  1. Identify Your Data Needs: Understand what specific information you need from the API and what information it is capable of giving. For example, if you order a pizza at Bob's Burgers, the kitchen is going to return an error because that is not a query parameter it knows how to handle.

  2. Use Vonage AI Studio's Interface: The studio offers an intuitive way to set parameters. For instance, if you’re fetching user data, you might set a parameter for the 'userID'.

  3. Input Your Parameter Values: These values should align with the data you're trying to retrieve. In our example, this would be the actual ID of the user.

Sample Data for API Request

Let's create a sample data set to visualize this:

{
  "order": {
    "item": "burger",
    "extras": ["cheese", "lettuce", "tomato"],
    "exclude": []
  }
}

Practical Example: Customizing Your Order in Vonage AI Studio

  1. Initial Order: Using the sample data, your initial order is a burger with cheese, lettuce, and tomato.

  2. Setting Parameters for Customization: Suppose you don't want pickles on your burger. In an API setting, such as Vonage AI Studio, you would add 'pickles' to the 'exclude' parameter.

  3. Modified Order Data:

    {
      "order": {
        "item": "burger",
        "extras": ["cheese", "lettuce", "tomato"],
        "exclude": ["pickles"]
      }
    }
  4. Outcome: The API processes this request and returns a response that reflects your customized order – a burger with cheese, lettuce, tomato, but without pickles.

Here is what it would look like in a database table:

Order ID Item Extras Exclude
001 Burger Cheese, Lettuce, Tomato Pickles

Join the Vonage's Burger "Guest List"

In this article, we learned the essentials of handling JSON objects, API responses, and response mapping within Vonage AI Studio, all through easy-to-grasp Bob's Burgers-inspired examples. If you liked what you read, join our "burger shop" of developers on Slack and follow us on X, formerly known as Twitter. If you found this blog helpful in any way, please share your review and tag me - I'd love to hear about your "dining experience!"

Diana PhamDeveloper Advocate

Diana is a developer advocate at Vonage. She likes eating fresh oysters.

Ready to start building?

Experience seamless connectivity, real-time messaging, and crystal-clear voice and video calls-all at your fingertips.

Subscribe to Our Developer Newsletter

Subscribe to our monthly newsletter to receive our latest updates on tutorials, releases, and events. No spam.