← Back Published on

Documenting JSON and XML

This week I took it upon myself to delve right into API Technical Writing, which is a broad topic in and of itself. I began learning more about data types and structured data, JSON, XML, and the tools that are needed to write in JSON and XML.

  • JSON (JavaScript Object Notation) - was initially created to hold structured data to be used in Javascript. It is currently the most popular way of sending data for Web APIs, but it can also be used for data for all kinds of applications.
  • XML (eXtensible Markup Language) - is a Hypertext Markup Language primarily used for creating websites. XML can be used for any kind of structured data.

To create JSON and XML files, you either need to use a basic text editor like NotePad or TextEdit. If you're feeling fancy, you can use more advanced text editors such as Atom or NotePad++ to format your files and validate your format.

To properly document JSON, I found it useful to be able to create and understand JSON files first. Before jumping right into creating JSON files, it's important to know some key things:

1. Basic Data Types consist of strings, numbers (integer or decimal), booleans (true or false), and null values (means nothing).  

2. Arrays are lists enclosed in square brackets and you can mix data types when using them. 

3. Objects are JSON's dictionaries. They're enclosed in curly brackets {}, keys/values are separated by a colon, and pairs are separated by commas. 

4. A rule of thumb for good JSON formatting is to add an indent for every new level of brackets, end lines with commas, and to use JSON formatting tools to make your life easier.

After reviewing the key data structure concepts, I created my own JSON file to capture weather forecast data for three days:

{

    "forecast": [
    {
        "Monday": {
            "description": "sunny",
            "maxTemp": 22,
            "minTemp": 20,
            "windSpeed": 12,
            "danger": false
        }
    },
    {
        "Tuesday": {
            "description": "windy",
            "maxTemp": 22,
            "minTemp": 20,
            "windSpeed": 40,
            "danger": true
        }
    },
    {
        "Wednesday": null
    }
    ]
}

//Side Note:  Free Formatter is an excellent tool to make sure your JSON formatting is correct!

In order to efficiently document JSON, a table with the element, description, type, and notes is needed. An element is the key of the key value or pair. The description is usually just a brief sentence or descriptive noun. The type is going to be one the data types from earlier (string, number, boolean). Lastly, the notes section is reserved for additional information. 

Meeting Request Table:

There was an API for an online calendar, and JSON was sent as a request to create a new meeting. Given the JSON request data, I created a documentation table to capture the information.  I made sure to include a concise description for each key element, as well as the data type, whether or not it was optional, and some additional notes when needed. Overall, I found documenting JSON to be straightforward and intuitive.

Next, I switched gears and moved onto documenting XML. I learned about the basic structure of XML, which included tags (similar to HTML), the content placed in between tags, and nest tags. Tags can have attributes, which are key/value pairs that hold simple data (a string). Once I got a general understanding of the XML, I replicated the steps in the previous section by creating my own XML file to capture weather forecast data.