From the course: Learn API Documentation with JSON and XML

JSON basics

From the course: Learn API Documentation with JSON and XML

Start my 1-month free trial

JSON basics

- [Instructor] Let's take a look at the data format known as JSON, which stands for JavaScript Object Notation. What is JSON? JavaScript is a programming language and JSON was originally created as a way you could hold structured data to be used in a JavaScript program. Once people saw how simple and elegant it was, it started being used in all kinds of applications. Right now, it's the most popular way for web APIs to send and receive data. JSON has a very small number of basic data types, as we discussed previously. Strings hold text and are enclosed in double quotation marks. Numbers can be either integer, that is whole numbers, or decimal and they can be positive, negative or zero. Booleans can be either true or false. Unlike strings, they do not have quotation marks around them. Finally, you can use null with no quotation marks to mean nothing. In addition to basic types, JSON has arrays and objects. Arrays are lists. They are enclosed in square brackets and have values with commas in between. Items in an array do not have to have all the same data type. So you can mix strings, numbers and so on. Here are some examples. The first is an array of numbers. The second is an array of strings. And the third is a mixed array, including a Boolean value and a null value. Objects are what JSON calls dictionaries, meaning that they consist of keys and values where you can look up a value given a key. Dictionaries are enclosed in curly brackets and the keys and values are separated by a colon. Each key and value pair are then separated by commas. Keys and values can be any data type, but string is the most common value for the key. Here's an example of an object that holds values for red, green and blue. You can see that the first key is the string red, which has a value of 205. These are separated by a colon. Then there is a comma and then the next key is green, with a value of 123. Everything is enclosed in curly brackets. And here's an example that holds some data about a person. There's a key for first name, last name and employed, where the names are of type string and the employed data is of type Boolean. Nesting means you've got arrays and objects inside of each other, creating multiple layers of collections. You can put arrays inside of objects, objects inside of arrays, arrays in arrays and so on. Sometimes a JSON file is one big object with lots of other objects and arrays inside of that one, top-level object.

Contents