Skip to main content

    Build foundational API knowledge

    Course overview
    Lesson
    4 min read

    Write event data in JSON

    In this lesson, we will walk through the specific format requirements for JSON data, see some examples of JSON data, and practice validating JSON formatting 

    Identify JSON syntax

    If you've ever looked through Klaviyo event data, you may notice that it's all written in a very specific format, with brackets and various indented levels.

    text
    
    {
      "order_id": "ab4903",
      "Items": [
        {
          "ProductName": "Dark Roast Coffee Beans",
          "Price": 30
        },
        {
          "ProductName": "Coffee Grinder - Silver",
          "Price": 15
        }
      ],
      "$value_currency": "USD",
      "value": 45
    }

    Klaviyo event data 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. Interpreting JSON is important for understanding GET requests, which return data formatted in JSON. Equally important is the ability to write JSON for sending POST requests, which require a JSON request body to send the proper data to the API.

    Interpret JSON formatting

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

    Note: You can access the JSON formatter from this video here.

    Explore important features of JSON formatting

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

    text
    
    POST https://a.klaviyocafe.com/api/lunch/sandwiches/burger/?add=cheese
    {
      "data": {
        "side": "french fries",
        "temperature": "well done",
        "toppings": [
          "lettuce",
          "tomato",
          "onion"
        ]
      },
      "quantity": 1
    }

    Let's go through the body of this POST request to 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.

    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: "this is a string."
    • Arrays in JSON are ordered collections of values enclosed within square brackets and separated by commas. Arrays can contain values of any data type, including other arrays and objects. Example: ["apple","orange","banana"]
    • Finally, numbers in JSON are the same as numbers outside of JSON. They do not need quotation marks, and are read as their numerical value.
    JSON body highlighting different data types

    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.

    In the above example, "data" is a JSON object nested within the API body. 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.

    Commas are used to separate individual key-value pairs within JSON objects. Place a comma between each key-value pair to separate them.

    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.

    Write event data in JSON