- [Instructor] What happens when you make a request
for JSON data and the data you receive
is different than what you expect?
What if the JSON is organized differently
than your code expects,
or it's simply not a valid response to your request?
To identify and deal with situations like these,
you 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.
Your code can compare the schema to the data
and verify either that the data is the type you expected
or that there's a problem that needs handling.
JSON Schema is a standard for creating schemas
for JSON data.
It was created by the internet engineering task force.
To create a schema using JSON Schema,
you create JSON using a specific structure and keywords
along with values you specify.
You can then write code to validate data against the schema.
What kinds of data problems can you 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 my app was requesting data
that included a numerical invoice number
and a billing total.
Now, imagine that I got this JSON as my response.
Well, first off, I noticed that
there's an invoiceNum property, but there's no property
that's obviously storing the billing total.
So, this data isn't exactly what I need,
because my code is expecting to work with a billing total.
By validating, I can keep from taking the performance hit
that comes with processing the data,
and then finding the error.
Instead, I can identify the error upfront
and then perhaps, resubmit my request or report the error.
Now, the other thing I noticed about this data
is that the invoiceNum value is a string
that includes both numbers and letters,
but I'm expecting a numerical value.
So, even though I 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 you.
A schema allows you to describe
the general structure of data,
but you 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 the 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.