In software, every piece of data has a data type. Learn about the common data types for numbers and text, as well as the Boolean type, which can have the values of true or false, and collections of data types, such as arrays and dictionaries. Finally, learn about structured data, where data types can hold one or more collections of other data types.
- [Presenter] Let's take a closer look at data types and structured data. As an API writer, one of the main tasks you'll need to do is describing data, so this lesson will explain how data is defined, especially for use in web APIs. When you are developing software, each piece of data has a type. Common types include integers, which are whole numbers, decimals, which can have digits to the right of the decimal place, text, true/false values, also known as Boolean, and so on.
You can also define your own custom types, like a type that captures all the information about a user, or a device, or a bank account, and so on. This course focuses on two formats: JSON and XML. I'll go into more detail about them later, but for now, I'll mention that JSON uses three basic types: numbers, strings, and Booleans, and XML only has one type, which is strings. Let me explain what all of this means. The word "string" is a technical term meaning "text." It's essentially a group of text characters that are strung together.
Usually, strings are enclosed in quotation marks, either with double or single quotes. They can have letters, numbers, punctuation, math symbols, and so on. They can even have spaces. Here are some examples. A single name, a code, and a sentence. Booleans may sound extremely technical, but they are, in fact, very simple. Long ago, a mathematician named George Boole came up with a radical idea. If we can do math with numbers, why can't we do math with logic? He came up with a number system with only two values: true and false.
With just these two values, you can do all sorts of things, just like you do with numbers. For documentation purposes, all you need to know is that a Boolean value can be either true or false. Let's do a little exercise here. This is a screenshot from a user profile. Once you make changes and submit it, it will send data back to the server. Of course, the data will have data types. Let's see if we can find all three of the data types that I've talked about. Well, string is easy. The current city and hometown are both strings.
Numbers are easy, too. The day and the year of the birthday are clearly numbers. What about Booleans? Well, any checkbox is a Boolean. It has a value of true if it's checked, and a value of false if it's not. In addition to individual data values, you can have groups of data called collections. There are two common types. Arrays are lists of data values, and dictionaries are what are called lookup tables. Let me explain each of these in more detail. Arrays, as I mentioned before, are a list of data values.
That means they have a size, which is how many are in the list, and they have an order, which describes which data values come first, second, and then third, and so on. For example, you could have a list of numbers, or you could have a list of strings. You can even have lists of lists. Dictionaries are a little more complicated. They're a list of data keys and values. The idea is you use the key to look up the value. Think about how you use a dictionary. Each word in the dictionary has a definition.
If your dictionary is electronic, then you give it the word, and it returns the definition. The word is the key, and the definition is the value. So, for example, let's say that you had data that divided the United States into several regions, and you wanted to have a dictionary where you could give it the region name, and it would return the number of U.S. states in that region. Your dictionary might look like this. In front of the colon is the key, a string in quotes, and after the colon is the value.
Structured data combines data types in collections. Collections can consist of other collections. In other words, you can have dictionaries of lists, a list of dictionaries, a dictionary of dictionaries, and so on. As many levels as you need. What you end up with is a tree structure where each collection is a branch and the end of each branch is a data value. You may be wondering what this has to do with APIs. The answer is very simple. As I mentioned in the introductory lesson, APIs have requests and responses. The responses almost always returned information from the server in the form of structured data, and sometimes the requests use structured data to pass information to the server.
- The purpose of documentation
- Data types
- Structured data
- Writing JSON and XML
- Documenting one-level JSON responses
- Documenting nested JSON responses
- XML attributes and examples
- Documenting XML
- Structuring data documentation