Skip to main content

    Make API calls to retrieve and manipulate data in your Klaviyo account

    Course overview
    Lesson
    1 min read

    Interpret and format API data

    Learn how to write and interpret JSON, or JavaScript Object Notation. This data format is used to send and receive API requests.

    Dissect Klaviyo API data

    When you make API calls, you may notice that all data is written in a very specific format, with brackets and various indented levels. For instance, the Get Profiles API endpoint may return profile data that looks like this:

    Example JSON profile data

    This is formatted in JSON, or JavaScript Object Notation. JSON is a data format used in Klaviyo event data and APIs, but it is also used all over the web for data storage and communication.

    Understanding JSON is crucial to making Klaviyo API calls. Any API requests that you send need to be formatted with JSON, and data that is returned via API will be returned as JSON data.

    In this lesson, we will:

    • Walk through the specific format requirements for JSON data
    • See some examples of JSON data
    • Practice validating JSON formatting

    Interpret JSON formatting

    Walk through an example of how a real Klaviyo event is formatted in JSON.

    You can access a JSON formatter here. This is slightly different from the one shown in the video, but works the same way.

    Explore important features of JSON formatting

    Recall from the Klaviyo Cafe example that ordering a burger was a POST request. This request looked like:

    POST https://a.klaviyocafe.com/api/lunch/sandwiches/burger/?add=cheese

    Body of an API request for a cheeseburger

    Click the arrow on each dropdown to go through the body of this POST request and dive into the specific syntax requirements of all JSON data.

    Key/value pairs

    JSON organizes data into key-value pairs, where each key is a string enclosed in double quotes, followed by a colon, and then its corresponding value. When you look up a key in JSON, you get the value.

    In the burger POST request, if you look up the key "quantity", you get 1.

    Cheeseburger payload with key-value pair labeled
    Data types

    JSON data is made up of objects, which are collections of key-value pairs enclosed in curly braces.

    JSON supports various data types as values of key-value pairs, including strings, numbers, arrays, and objects.

    • Any text value is represented as a string and must be enclosed within double quotes. Example: "french fries."
    • Arrays in JSON represent lists of values enclosed within square brackets and separated by commas. Arrays can contain values of any data type, including other arrays and objects. Example: [ "lettuce", "tomato", "onion" ]
    • Numbers in JSON are the same as numbers outside of JSON. They are not wrapped in quotation marks, and are read as their numerical value.
    • Finally, JSON objects are collections of key-value pairs enclosed in curly braces. Key-value pairs are separated by commas.
    Different data types highlighted in example JSON
    Indentation and nested data

    Indentation refers to the practice of formatting JSON data by adding spaces or tabs to visually organize the structure of the data. While indentation is not required for JSON to be valid, it makes it much easier to read and understand the relationships between different elements.

    You can think of JSON indentation like writing an outline for a research paper. Much like how this outline is divided into sections, subsections, and sub-subsections, JSON data is organized into nested levels. In both cases, this hierarchical structure allows for clear organization and easy navigation of the content.

    Comparison of paper outline and JSON structure

    In the above example, requests is a JSON object nested within the data object:

    Requests object nested within data object

    JSON allows objects and arrays to be nested within one another, with each nested value indented by one level. This means that values within JSON can themselves be objects or arrays, forming a hierarchical or nested data structure. For example, an object can contain another object as one of its values, or an array can contain other arrays or objects.

    Just as headings and subheadings in a research paper outline provide a roadmap for understanding the paper's content, the indentation and nesting in JSON provide a visual guide for understanding the relationships between different pieces of data.

    Commas

    Using commas correctly in JSON is essential for creating valid and properly formatted data structures. Commas are one of the most common causes of syntax errors when writing JSON.

    In JSON objects, commas are used to separate key-value pairs. Remember to add a comma after each pair, except the final one.

    Example JSON highlighting lack of comma after the last key-value pair

    JSON does not allow commas after the last element in an array or the last key-value pair in an object. These are called trailing commas and can lead to syntax errors. The Klaviyo API will tell you if there is a parse error, and Postman will put a red underline (like a spelling mistake) under any incorrect commas to help you troubleshoot the issue.

    Interpret and format API data