A schema allows you to specify what a valid JSON string should look like, and enables you to programmatically check a JSON string against it to tell if that string is valid for your purposes. This video explores how a schema is useful with JSON, and what a schema can do.
- [Instructor] In front-end development we write a lot of code that works with JSON data that we request and receive from different sources, but what happens if the data you receive is different than what you expect? What if the JSON is organized differently than your code expects or is simply not a valid response to your request? To identify and deal with situations like these, we can use a schema. A schema is essentially a blueprint for what data should look like. You can create a schema for the data you expect to receive and then provide that schema to the code that handles the data.
What kinds of problems can we identify with JSON Schema? The specification has a number of options, but some of the most common areas include missing required data and incorrect types of data. For instance, imagine if your app was requesting data that included a numerical invoice number and a billing total. Now, imagine that you've got this JSON as your response. Well, first off, we notice that there's an invoiceNum property but no property that's obviously storing the billing total, so this data isn't exactly what we need because our code is expecting to work with a billing total.
By validating, we can keep from taking the performance hit that comes with processing the data and then finding the error. Instead, we can identify the error up front and then perhaps resubmit our request or report the error. Now, the other thing we can notice about this data is that the invoiceNum value is a string that includes both numbers and letters, but we are expecting a numerical value, so even though we have data, it's of the wrong data type. Again, validating the data before processing it prevents potential errors or unexpected results that might come from trying to perform mathematical operations on a string value.
It's also important to be clear about what a schema can't do for us. A schema allows us to describe the general structure of data but we cannot use a schema to really delve into the meaning of the data itself to decide whether it makes sense. For instance, suppose you have JSON data for something like a shopping cart that includes a number of items and their prices along with tax, shipping, and an order total. You can use a schema to verify that the price, tax, shipping, and total values are all numbers.
However, a schema does not allow you to verify that, for instance, the total value is equal to the sum of the item prices and the tax and shipping, or even that the total value is greater than each of its component values. For this kind of validation, you have to write code that works with and compares values.
- Setting up the environment
- Understanding JSON data
- Preventing data theft
- Returning readable JSON
- Testing for an empty object
- Creating a basic schema with JSON schema
- Validating JSON data against a schema
- Converting between JSON and XML
- Converting between JSON and YAML