Return to Video

04-02-json-demo.mp4

  • 0:00 - 0:04
    In this video, we'll see a demonstration of JSON data.
  • 0:04 - 0:05
    As a reminder, JSON stands for
  • 0:05 - 0:08
    Java Script Object Notation, and
  • 0:08 - 0:09
    it's a standard for writing
  • 0:09 - 0:13
    data objects into human readable format, typically in a file.
  • 0:13 - 0:16
    It's useful for exchanging data
  • 0:16 - 0:18
    between programs, and generally
  • 0:18 - 0:20
    because it's quite flexible, it's useful
  • 0:20 - 0:23
    for representing and for storing data that's semi-structured.
  • 0:23 - 0:24
    A reminder of the
  • 0:24 - 0:26
    basic constructs in JSON, we
  • 0:26 - 0:28
    have the atomic value, such
  • 0:28 - 0:30
    as integers and strings and so on.
  • 0:30 - 0:31
    And then we have two types of
  • 0:31 - 0:33
    composite things; we have
  • 0:33 - 0:34
    objects that are sets of
  • 0:34 - 0:38
    label-value pairs and then we have arrays that are lists of values.
  • 0:38 - 0:40
    In the demonstration, we'll go through
  • 0:40 - 0:41
    in more detail the basic constructs
  • 0:41 - 0:44
    of JSON and we'll look at
  • 0:44 - 0:46
    some tactic correctness, we'll demonstrate
  • 0:46 - 0:47
    the flexibility of the data
  • 0:47 - 0:49
    model and then we'll
  • 0:49 - 0:50
    look briefly at JSON's schema,
  • 0:50 - 0:52
    not widely used yet but
  • 0:52 - 0:54
    still fairly interesting to look at
  • 0:54 - 0:55
    and we'll look at some validation
  • 0:55 - 0:59
    of JSON data against a particular schema.
  • 0:59 - 1:00
    So, here's the JSON
  • 1:00 - 1:03
    data that we're gonna be working with during this demo.
  • 1:03 - 1:04
    It's the same data that appeared
  • 1:04 - 1:07
    in the slides, in the introduction
  • 1:07 - 1:08
    to JSON, but now we're going
  • 1:08 - 1:11
    to look into the components of the data.
  • 1:11 - 1:13
    It's also by the way, the
  • 1:13 - 1:14
    same example pretty much that we
  • 1:14 - 1:17
    used for XML, it's reformatted
  • 1:17 - 1:18
    of course to meet the JSON
  • 1:18 - 1:22
    data model, but you can compare the two directly.
  • 1:22 - 1:23
    Lastly, we do have
  • 1:23 - 1:25
    the file for the data on
  • 1:25 - 1:26
    the website, and I do
  • 1:26 - 1:28
    suggest that you download the
  • 1:28 - 1:29
    file so that you can
  • 1:29 - 1:31
    take a look at it closely on your own computer.
  • 1:31 - 1:32
    All right.
  • 1:32 - 1:33
    So, let's see what we have,
  • 1:33 - 1:34
    right now we're in
  • 1:34 - 1:36
    an editor for JSON data.
  • 1:36 - 1:38
    It happens to be the Eclipse
  • 1:38 - 1:38
    editor and we're going to
  • 1:38 - 1:39
    make make some edits to the
  • 1:39 - 1:41
    file after we look through
  • 1:41 - 1:43
    the constructs of the file.
  • 1:43 - 1:45
    So, this is JSON
  • 1:45 - 1:48
    data representing books and
  • 1:48 - 1:49
    magazines, and we have
  • 1:49 - 1:52
    a little more information about our books and our magazines.
  • 1:52 - 1:53
    So, at the outermost, the
  • 1:53 - 1:57
    curly brace indicates that this is a JSON object.
  • 1:57 - 1:59
    And as a reminder, an object
  • 1:59 - 2:01
    is a set of label-value
  • 2:01 - 2:03
    pairs, separated by commas.
  • 2:03 - 2:07
    So, our first value is the label "books". And
  • 2:07 - 2:09
    then our first element in
  • 2:09 - 2:11
    the object is the label books
  • 2:11 - 2:14
    and this big value and the
  • 2:14 - 2:16
    second, so there's only two label-value
  • 2:16 - 2:17
    pairs here, is the
  • 2:17 - 2:21
    label magazines and this big value here.
  • 2:21 - 2:23
    And let's take a look first at magazines.
  • 2:23 - 2:25
    So magazines, again, is the
  • 2:25 - 2:26
    label and the value we
  • 2:26 - 2:27
    can see with the square
  • 2:27 - 2:30
    brackets here is an array.
  • 2:30 - 2:31
    An array is a list of
  • 2:31 - 2:33
    values and here we
  • 2:33 - 2:35
    have two values in our array.
  • 2:35 - 2:37
    They're still composite values.
  • 2:37 - 2:38
    So, we have two values, each
  • 2:38 - 2:40
    of which is an object,
  • 2:40 - 2:42
    a set of label-value pairs.
  • 2:42 - 2:46
    Let me mention, sometimes people call these labels 'properties', by the way.
  • 2:46 - 2:48
    Okay. So, now we are inside
  • 2:48 - 2:49
    our 2 objects that are
  • 2:49 - 2:53
    the 2 elements in the array that's the value of magazines.
  • 2:53 - 2:54
    And each one of those has
  • 2:54 - 2:56
    3 labels and 3 values.
  • 2:56 - 2:58
    And now we're finally down to the base values.
  • 2:58 - 3:00
    So, we have the title being "National
  • 3:00 - 3:02
    Geographic", a string, the
  • 3:02 - 3:04
    month being January, a string
  • 3:04 - 3:06
    and the year 2009, where 2009 is an integer.
  • 3:06 - 3:08
    And again, we have
  • 3:08 - 3:12
    another object here that's a different magazine
  • 3:12 - 3:15
    with a different name, month and happens to be the same year.
  • 3:15 - 3:16
    Now, these two have exactly the
  • 3:16 - 3:18
    same structure but they don't
  • 3:18 - 3:19
    have to and we will
  • 3:19 - 3:21
    see that as we start editing the file.
  • 3:21 - 3:23
    But before we edit the file,
  • 3:23 - 3:24
    let's go and look at
  • 3:24 - 3:26
    our books here.
  • 3:26 - 3:28
    The value of our other
  • 3:28 - 3:30
    label-value pair inside the
  • 3:30 - 3:32
    outermost object, "books" is
  • 3:32 - 3:34
    also an array, and
  • 3:34 - 3:35
    the array in this case also
  • 3:35 - 3:38
    has just two elements, so we've represented two books here.
  • 3:38 - 3:40
    It's a little more complicated than the
  • 3:40 - 3:42
    magazines, but those elements
  • 3:42 - 3:45
    are still objects that are label-value pairs.
  • 3:45 - 3:47
    So, we have now the ISBN,
  • 3:47 - 3:49
    the price, the addition, the title,
  • 3:49 - 3:51
    all either integers or strings,
  • 3:51 - 3:54
    and then we have one nested composite
  • 3:54 - 3:56
    object which is the authors
  • 3:56 - 3:57
    and that's an array again.
  • 3:57 - 4:02
    So, the array again, is indicated by the square brackets.
  • 4:02 - 4:04
    And inside this array, we
  • 4:04 - 4:06
    have two authors and each
  • 4:06 - 4:07
    of the authors has a first
  • 4:07 - 4:08
    name and a last name,
  • 4:08 - 4:10
    but again, that uniformity is
  • 4:10 - 4:13
    not required by the model itself, as we'll see.
  • 4:13 - 4:15
    So, as I mentioned,
  • 4:15 - 4:16
    this is actually an editor for
  • 4:16 - 4:19
    JSON data and we're going to come back to this editor in a moment.
  • 4:19 - 4:20
    But what I wanted to do is
  • 4:20 - 4:22
    show the same data
  • 4:22 - 4:23
    in a browser because browsers
  • 4:23 - 4:25
    actually offer some nice features
  • 4:25 - 4:27
    for navigating in JSON.
  • 4:27 - 4:28
    So here we are in the
  • 4:28 - 4:30
    Chrome browser, which has nice
  • 4:30 - 4:32
    features for navigating JSON,
  • 4:32 - 4:34
    and other browsers do as well.
  • 4:34 - 4:35
    We can see here again that we
  • 4:35 - 4:37
    have an object in
  • 4:37 - 4:39
    our JSON data, that consists
  • 4:39 - 4:40
    of two label-value pairs;
  • 4:40 - 4:42
    books and magazines, which are
  • 4:42 - 4:43
    currently closed and and then
  • 4:43 - 4:47
    this plus allows us to open them up, and see the structure.
  • 4:47 - 4:48
    For example, we open magazines
  • 4:48 - 4:52
    and we see that magazines is an array containing two objects.
  • 4:52 - 4:53
    We can open one of those
  • 4:53 - 4:55
    objects, and see that the three label-value pairs.
  • 4:55 - 4:59
    Now we're at the lowest levels and similarly for the other object.
  • 4:59 - 5:00
    We can see here that Books
  • 5:00 - 5:03
    is also an array, and we go ahead and open it up.
  • 5:03 - 5:05
    It's an array of two objects.
  • 5:05 - 5:06
    We open one of those
  • 5:06 - 5:07
    objects and we see again
  • 5:07 - 5:09
    the set of label-value pairs,
  • 5:09 - 5:10
    where one of the values
  • 5:10 - 5:12
    is a further nesting.
  • 5:12 - 5:14
    It's an array and we open
  • 5:14 - 5:15
    that array, and we see
  • 5:15 - 5:16
    two objects, and we open
  • 5:16 - 5:19
    them and finally see the data at the lowest levels.
  • 5:19 - 5:21
    So again, the browser
  • 5:21 - 5:22
    here gives us a nice way
  • 5:22 - 5:26
    to navigate the JSON data and see its structure.
  • 5:26 - 5:28
    So now we're back to our JSON editor.
  • 5:28 - 5:30
    By the way, this editor, Eclipse, does
  • 5:30 - 5:32
    also have some features for
  • 5:32 - 5:34
    opening and closing the structure
  • 5:34 - 5:35
    of the data, but it's
  • 5:35 - 5:38
    not quite as nice as the browser that we use.
  • 5:38 - 5:39
    So we decided to use the browser instead.
  • 5:39 - 5:40
    What we are going to
  • 5:40 - 5:42
    use the editor for is to
  • 5:42 - 5:43
    make some changes to the
  • 5:43 - 5:44
    JSON data and see which
  • 5:44 - 5:47
    changes are legal and which aren't.
  • 5:47 - 5:50
    So, let's take a look at the first change, a very simple one.
  • 5:50 - 5:52
    What if we forgot a comma.
  • 5:52 - 5:53
    Well, when we try to
  • 5:53 - 5:54
    save that file, we get a
  • 5:54 - 5:55
    little notice that we have an
  • 5:55 - 5:56
    error, we expected an
  • 5:56 - 5:58
    N value, so that's a
  • 5:58 - 6:02
    pretty straightforward mistake, let's put that comma back.
  • 6:02 - 6:04
    Let's say insert an
  • 6:04 - 6:07
    extra brace somewhere here, for whatever reason.
  • 6:07 - 6:09
    We accidentally put in an extra brace.
  • 6:09 - 6:13
    Again we see that that's marked as an error.
  • 6:13 - 6:13
    So an error that can
  • 6:13 - 6:15
    be fairly common to make is
  • 6:15 - 6:18
    to forget to put quotes around strings.
  • 6:18 - 6:20
    So, for example, this ISBN
  • 6:20 - 6:23
    number here, if we don't quote it, we're gonna get an error.
  • 6:23 - 6:24
    As we'll see the only things that can
  • 6:24 - 6:27
    be unquoted are numbers and
  • 6:27 - 6:29
    the values null, true and false.
  • 6:29 - 6:31
    So, let's put our quotes back there.
  • 6:31 - 6:33
    Now, actually, even more
  • 6:33 - 6:34
    common is to forget to
  • 6:34 - 6:37
    put quotes around the labels in label-value pairs.
  • 6:37 - 6:40
    But if we forget to quote that, that's going to be an error as well.
  • 6:40 - 6:41
    You might have noticed, by the
  • 6:41 - 6:43
    way, when we use the browser
  • 6:43 - 6:44
    that the browser didn't even show
  • 6:44 - 6:46
    us the quotes in the labels.
  • 6:46 - 6:47
    But you do when you make
  • 6:47 - 6:51
    the raw JSON data, you do need to include those quotes.
  • 6:51 - 6:56
    Speaking of quotes, what if we quoted our price here.
  • 6:56 - 6:57
    Well that's actually not an
  • 6:57 - 6:58
    error, because now we've simply turned
  • 6:58 - 7:00
    price into a string, and
  • 7:00 - 7:03
    string values are perfectly well allowed anywhere.
  • 7:03 - 7:04
    Now we'll see when we use
  • 7:04 - 7:05
    JSON's schema that we
  • 7:05 - 7:07
    can make restrictions that don't allow
  • 7:07 - 7:08
    strings in certain places, but
  • 7:08 - 7:10
    just for syntactic correctness of
  • 7:10 - 7:15
    JSON data any of our values can be strings.
  • 7:15 - 7:16
    Now, as I mentioned, there are
  • 7:16 - 7:17
    a few values that are
  • 7:17 - 7:20
    sort of reserved words in JSON.
  • 7:20 - 7:22
    For example, true is a
  • 7:22 - 7:24
    reserved word for a bullion value.
  • 7:24 - 7:25
    That means we don't need to
  • 7:25 - 7:27
    quote it because it's actually
  • 7:27 - 7:28
    its own special type of value.
  • 7:28 - 7:30
    And so is false.
  • 7:30 - 7:32
    And the third one is null,
  • 7:32 - 7:35
    so there's a built-in concept of null.
  • 7:35 - 7:36
    Now, if we wanted to
  • 7:36 - 7:38
    use nil for whatever reason
  • 7:38 - 7:39
    instead of null, well, now
  • 7:39 - 7:40
    we're going to get an error because
  • 7:40 - 7:42
    nil is not a reserved word,
  • 7:42 - 7:43
    and if we really wanted nil
  • 7:43 - 7:47
    then we would need to actually make it a quoted string.
  • 7:47 - 7:50
    Now, let's take a look inside our author list.
  • 7:50 - 7:51
    And I'm going to show you
  • 7:51 - 7:52
    that arrays do not have
  • 7:52 - 7:54
    to have the same type of
  • 7:54 - 7:56
    value for every element in the array.
  • 7:56 - 7:58
    So here we have a homogeneous
  • 7:58 - 7:59
    list of authors. Both of them
  • 7:59 - 8:01
    are objects with a first
  • 8:01 - 8:02
    name and a last name as
  • 8:02 - 8:04
    separate label-value pairs,
  • 8:04 - 8:05
    but if I change that
  • 8:05 - 8:07
    first one, the entire value
  • 8:07 - 8:09
    to be, instead of a
  • 8:09 - 8:11
    composite one, simply the string,
  • 8:11 - 8:13
    Jefferey Ullman. Oops, sorry
  • 8:13 - 8:15
    about my typing there, and that
  • 8:15 - 8:17
    is not an error, it
  • 8:17 - 8:18
    is allowed to have a string,
  • 8:18 - 8:20
    and then a composite object.
  • 8:20 - 8:22
    And we could even have an array, and anything we want.
  • 8:22 - 8:24
    In an array, when you
  • 8:24 - 8:25
    have a list of values, all
  • 8:25 - 8:26
    you need is for each one
  • 8:26 - 8:30
    to be syntactically a correct value in JSON.
  • 8:30 - 8:32
    Now let's go visit our magazines
  • 8:32 - 8:33
    for a moment here and let
  • 8:33 - 8:35
    me show that empty objects are okay.
  • 8:35 - 8:37
    So a list of label
  • 8:37 - 8:41
    value pairs, comprising an object, can be the empty list.
  • 8:41 - 8:42
    And so now I've turned this magazine
  • 8:42 - 8:44
    into having no information about
  • 8:44 - 8:46
    it, but that is legal in JSON.
  • 8:46 - 8:50
    And similarly, arrays are allowed to be of zero length.
  • 8:50 - 8:52
    So I can take these authors
  • 8:52 - 8:53
    here and I can just take
  • 8:53 - 8:54
    out all of the authors, and
  • 8:54 - 8:58
    make that an empty list, but that's still valid JSON.
  • 8:58 - 9:01
    Now, what if I took this array out altogether?
  • 9:01 - 9:02
    In that case, now we
  • 9:02 - 9:04
    have an error because this is
  • 9:04 - 9:05
    an object where we have
  • 9:05 - 9:08
    label-value pairs and every
  • 9:08 - 9:09
    label-value pair has to
  • 9:09 - 9:12
    have both a label and a value.
  • 9:12 - 9:13
    So let's put our array back
  • 9:13 - 9:15
    and we can have anything in
  • 9:15 - 9:16
    there so let's just make it
  • 9:16 - 9:19
    "fu" and that corrects the error.
  • 9:19 - 9:20
    What if we didn't want an
  • 9:20 - 9:21
    array here instead and we
  • 9:21 - 9:24
    tried to make it, say, an object,?
  • 9:24 - 9:26
    Well, we're going to see an
  • 9:26 - 9:28
    error there, because an object
  • 9:28 - 9:29
    as a reminder and this is an
  • 9:29 - 9:30
    easy mistake to make. Objects
  • 9:30 - 9:33
    are always label-value pairs.
  • 9:33 - 9:34
    So if you want just a value,
  • 9:34 - 9:36
    that should be an array if
  • 9:36 - 9:37
    you want an object, then we're
  • 9:37 - 9:39
    talking about a label-value pair, so
  • 9:39 - 9:40
    we can just add "fu" as
  • 9:40 - 9:42
    our value, and then we're all set.
  • 9:42 - 9:46
    So what we've seen so far is syntactic correctness.
  • 9:46 - 9:48
    Again, there's no required
  • 9:48 - 9:50
    uniformity across values in
  • 9:50 - 9:52
    arrays or in the
  • 9:52 - 9:55
    label-value pairs in objects we
  • 9:55 - 9:56
    just need to ensure that
  • 9:56 - 9:57
    all of our values, our basic
  • 9:57 - 9:59
    values, are of the right types,
  • 9:59 - 10:00
    and things like our commas and
  • 10:00 - 10:02
    curly braces are all in place.
  • 10:02 - 10:04
    What we're gonna do next is look
  • 10:04 - 10:05
    at JSON's schema where we
  • 10:05 - 10:08
    have a mechanism for enforcing certain
  • 10:08 - 10:11
    constraints beyond simple syntactic correctness.
  • 10:11 - 10:13
    If you've been very observant, you
  • 10:13 - 10:14
    might even have noticed that we
  • 10:14 - 10:15
    have a second tab up
  • 10:15 - 10:17
    here in our editor for a
  • 10:17 - 10:18
    second JSON file, and this file
  • 10:18 - 10:20
    is going to be the schema
  • 10:20 - 10:22
    for our bookstore data. We're using
  • 10:22 - 10:25
    JSON schema, and JSON
  • 10:25 - 10:27
    schema, like, XML schema
  • 10:27 - 10:29
    is expressed in the data model itself.
  • 10:29 - 10:31
    So, our schema description for
  • 10:31 - 10:33
    this JSON data is itself
  • 10:33 - 10:35
    JSON data, and here it is.
  • 10:35 - 10:37
    And it's going to take a bit of time to explain.
  • 10:37 - 10:37
    Now the first thing that you might
  • 10:37 - 10:39
    notice is wow, the schema
  • 10:39 - 10:41
    looks more complicated and in
  • 10:41 - 10:43
    fact longer than the data itself.
  • 10:43 - 10:47
    Well, that is true, but that's mostly because our data file is tiny.
  • 10:47 - 10:49
    So, if we had thousands, you know, tens
  • 10:49 - 10:51
    of thousands of books and magazines,
  • 10:51 - 10:53
    our schema file wouldn't
  • 10:53 - 10:54
    change, but our data file would
  • 10:54 - 10:57
    be much longer and that's the typical case, in reality.
  • 10:57 - 10:58
    Now, this video is not a
  • 10:58 - 11:01
    complete tutorial about JSON's schema.
  • 11:01 - 11:02
    There's many constructs in JSON's
  • 11:02 - 11:04
    schema that weren't needed to
  • 11:04 - 11:06
    describe the bookstore data, for example.
  • 11:06 - 11:08
    And even this file here,
  • 11:08 - 11:11
    I'm not gonna go through every detail of it right here.
  • 11:11 - 11:12
    You can download the file and
  • 11:12 - 11:15
    take a look, read a little more about JSON schema.
  • 11:15 - 11:16
    I'm just going to give the
  • 11:16 - 11:17
    flavor of the schema
  • 11:17 - 11:19
    specification and then we're
  • 11:19 - 11:20
    going to work with validating the data
  • 11:20 - 11:24
    itself to see how the schema and data work together.
  • 11:24 - 11:28
    But to give you the flavor here, let's go through at least some portions of the schema.
  • 11:28 - 11:29
    So, in some sense,
  • 11:29 - 11:31
    the structure of the schema file
  • 11:31 - 11:34
    reflects the structure of the data file that it's describing.
  • 11:34 - 11:37
    So, the outermost constructs in
  • 11:37 - 11:38
    the schema file are the
  • 11:38 - 11:39
    outermost in the data file and
  • 11:39 - 11:42
    as we nest it parallels the nesting.
  • 11:42 - 11:43
    Let me just show a little
  • 11:43 - 11:48
    bit here, we'll probably look at most of it in the context of validation.
  • 11:48 - 11:52
    So, we see here that our outermost construct in our data file is an object.
  • 11:52 - 11:53
    And that's told to us,
  • 11:53 - 11:55
    because we have "type" as
  • 11:55 - 11:57
    one of our built-in labels for the schema.
  • 11:57 - 11:58
    So we we have an
  • 11:58 - 12:00
    object with two properties, as
  • 12:00 - 12:02
    we can see here, the book's property
  • 12:02 - 12:04
    and the magazine's property.
  • 12:04 - 12:05
    And I use the word
  • 12:05 - 12:07
    "labels" frequently for label-value
  • 12:07 - 12:11
    pairs, that's synonymous with property value pairs.
  • 12:11 - 12:13
    Then inside the books property
  • 12:13 - 12:15
    for example, we see that
  • 12:15 - 12:16
    the type of that is array,
  • 12:16 - 12:19
    so we've got a label-value pair where the value is an array.
  • 12:19 - 12:22
    And then we follow the nesting and see that it's an array of objects.
  • 12:22 - 12:24
    And we go further down and we
  • 12:24 - 12:26
    see the different label-value pairs
  • 12:26 - 12:27
    of the object that make up
  • 12:27 - 12:31
    the books and nesting further into the authors and so on.
  • 12:31 - 12:32
    We see similarly for magazines
  • 12:32 - 12:34
    that the value of the
  • 12:34 - 12:36
    a label-value pair for
  • 12:36 - 12:37
    magazines is an array, and
  • 12:37 - 12:41
    that array consists of objects with further nesting.
  • 12:41 - 12:42
    So what we're looking at here is
  • 12:42 - 12:45
    an online JSON schema validator. We have two windows.
  • 12:45 - 12:46
    On the left we have our
  • 12:46 - 12:47
    schema and on the
  • 12:47 - 12:49
    right we have our data, and
  • 12:49 - 12:50
    this is exactly the same data
  • 12:50 - 12:54
    file and schema file that we were looking at earlier.
  • 12:54 - 12:55
    If we hit the validate button,
  • 12:55 - 12:58
    hopefully everything should work and it does.
  • 12:58 - 12:59
    This tells us that the
  • 12:59 - 13:03
    JSON data is valid with respect to the schema.
  • 13:03 - 13:04
    Now, this system will of
  • 13:04 - 13:06
    course find basic syntactic errors
  • 13:06 - 13:07
    so I can take away a comma
  • 13:07 - 13:09
    just like I did before and
  • 13:09 - 13:10
    when I validate I'll get a
  • 13:10 - 13:13
    parsing error that really has nothing to do with the schema.
  • 13:13 - 13:14
    What I'm going to focus on
  • 13:14 - 13:16
    now is actually validating
  • 13:16 - 13:18
    semantic correctness of the Jason
  • 13:18 - 13:19
    with respect back to the constructs
  • 13:19 - 13:21
    that we've specified in this schema.
  • 13:21 - 13:25
    Let me first put that comma back so we start with a valid file.
  • 13:25 - 13:26
    So, the first thing I'll show is
  • 13:26 - 13:28
    the ability to constrain basic
  • 13:28 - 13:29
    types, and then the ability
  • 13:29 - 13:32
    to constrain the range of values of those basic types.
  • 13:32 - 13:34
    And let's focus on price.
  • 13:34 - 13:35
    So here we're talking about the
  • 13:35 - 13:37
    price property inside books and
  • 13:37 - 13:39
    we specify in our schema
  • 13:39 - 13:42
    that the type of the price must be an integer.
  • 13:42 - 13:44
    So, for example, if our
  • 13:44 - 13:46
    price were instead a string
  • 13:46 - 13:47
    and we went ahead and try
  • 13:47 - 13:49
    to validate that we would get an error.
  • 13:49 - 13:51
    Let's make it back into an
  • 13:51 - 13:53
    integer but let's make
  • 13:53 - 13:56
    it into the integer 300 now instead of 100.
  • 13:56 - 13:58
    And why am I doing that?
  • 13:58 - 14:00
    Because the JSON schema also
  • 14:00 - 14:01
    lets me constrain the range of
  • 14:01 - 14:05
    values that are allowed if we have a numeric value.
  • 14:05 - 14:06
    So, not only in price did I
  • 14:06 - 14:08
    say that it's an integer but
  • 14:08 - 14:09
    I also said that it
  • 14:09 - 14:11
    has a minimum and maximum value,
  • 14:11 - 14:13
    the integer of prices must
  • 14:13 - 14:15
    be between 0 and 200.
  • 14:15 - 14:16
    So, if I try to make
  • 14:16 - 14:18
    the price of 300, and I
  • 14:18 - 14:20
    validate, I'm again getting an error.
  • 14:20 - 14:21
    Now it's not a type error,
  • 14:21 - 14:23
    but it's an error that my
  • 14:23 - 14:26
    integer was outside of the allowed range.
  • 14:26 - 14:27
    I've put the price back to
  • 14:27 - 14:28
    a hundred, and now let's
  • 14:28 - 14:32
    look at constraints on string values.
  • 14:32 - 14:33
    JSON schema actually has
  • 14:33 - 14:35
    a little pattern matching language that
  • 14:35 - 14:36
    can be used to constrain the
  • 14:36 - 14:40
    allowable strings for a specific type of value.
  • 14:40 - 14:43
    We'll look at ISBN number here as an example of that.
  • 14:43 - 14:45
    We've said that ISBN is
  • 14:45 - 14:47
    of type string, and then
  • 14:47 - 14:48
    we've further constrained in the
  • 14:48 - 14:50
    schema that the string values for
  • 14:50 - 14:52
    ISBN must satisfy a certain pattern.
  • 14:52 - 14:56
    I'm not gonna go into the details of this pattern-matching language.
  • 14:56 - 14:57
    I'm just gonna give an example.
  • 14:57 - 14:59
    And in fact, this entire demo is
  • 14:59 - 15:00
    really just an example lots of
  • 15:00 - 15:03
    things in JSON's schema that we're not seeing.
  • 15:03 - 15:05
    What this pattern here says is
  • 15:05 - 15:06
    that the string value for
  • 15:06 - 15:08
    ISBN must start with
  • 15:08 - 15:13
    the four characters ISBN and then can be followed by anything else.
  • 15:13 - 15:14
    So, if we go over to our
  • 15:14 - 15:15
    data and we look at
  • 15:15 - 15:17
    the ISBN number here and
  • 15:17 - 15:18
    say we have a typo, we
  • 15:18 - 15:20
    forgot the "I" and we try to validate.
  • 15:20 - 15:22
    Then we'll see that our data
  • 15:22 - 15:25
    no longer matches our schema specification.
  • 15:25 - 15:29
    Now let's look at some other constraints we can specify in JSON's schema.
  • 15:29 - 15:32
    We can constrain the number of elements in an array.
  • 15:32 - 15:35
    We can give a minimum or maximum or both.
  • 15:35 - 15:38
    And I've done that here in the context of the authors array.
  • 15:38 - 15:39
    Remember the authors are
  • 15:39 - 15:40
    an array that's a list of
  • 15:40 - 15:42
    objects and here I've said that
  • 15:42 - 15:44
    we have a minimum number of
  • 15:44 - 15:45
    items of 1 and a
  • 15:45 - 15:46
    maximum number items of 10.
  • 15:46 - 15:48
    In other words, every book
  • 15:48 - 15:51
    has to have between one and ten authors.
  • 15:51 - 15:53
    So let's try, for example,
  • 15:53 - 15:56
    taking out all of our authors here in our first book.
  • 15:56 - 15:57
    We actually looked at this before in terms
  • 15:57 - 15:59
    of syntactic validity, and it
  • 15:59 - 16:01
    was perfectly valid to have an empty array.
  • 16:01 - 16:02
    But when we try to validate
  • 16:02 - 16:03
    now we do get an
  • 16:03 - 16:05
    error, and the reason is
  • 16:05 - 16:06
    that we said that we needed
  • 16:06 - 16:10
    between one and ten array elements in the case of authors.
  • 16:10 - 16:12
    Now let's fix that,
  • 16:12 - 16:13
    not by putting our authors back
  • 16:13 - 16:14
    but let's say we actually decide
  • 16:14 - 16:17
    we would like to be able to have books that have no authors.
  • 16:17 - 16:19
    So, we can simply fix
  • 16:19 - 16:21
    that by changing that minimum
  • 16:21 - 16:23
    item to zero and that
  • 16:23 - 16:24
    makes our data valid again and
  • 16:24 - 16:26
    in fact, we could actually take that
  • 16:26 - 16:28
    minimum constraint out all together,
  • 16:28 - 16:32
    and if we do that our data is still going to be valid.
  • 16:32 - 16:33
    Now let's see what happens when we
  • 16:33 - 16:36
    add something to our data that isn't mentioned in the schema.
  • 16:36 - 16:38
    If you look carefully you'll see
  • 16:38 - 16:39
    that everything that we have
  • 16:39 - 16:42
    in the data so far has been specified in the schema.
  • 16:42 - 16:43
    Let's say we come along
  • 16:43 - 16:46
    and decide were gonna also have ratings for our books.
  • 16:46 - 16:47
    So let's add here a
  • 16:47 - 16:51
    rating label property with the value 5.
  • 16:51 - 16:53
    We go ahead and validate, you
  • 16:53 - 16:54
    probaly think it's not going to
  • 16:54 - 16:57
    validate properly but actually it did.
  • 16:57 - 16:59
    The definition of JSON
  • 16:59 - 17:00
    schema that it can constrain things by
  • 17:00 - 17:02
    describing them but you
  • 17:02 - 17:04
    can also have components in
  • 17:04 - 17:06
    the data that aren't present in this schema.
  • 17:06 - 17:08
    If we want to insist
  • 17:08 - 17:10
    that every property that is
  • 17:10 - 17:11
    present in the data is
  • 17:11 - 17:12
    also described in this
  • 17:12 - 17:14
    schema, then we can
  • 17:14 - 17:17
    actually add a constraint to the schema that tells us that.
  • 17:17 - 17:20
    Specifically, under the object
  • 17:20 - 17:22
    here, we can put in
  • 17:22 - 17:24
    a special flag which itself
  • 17:24 - 17:27
    is specified as a label called additional properties.
  • 17:27 - 17:29
    And this flag if we
  • 17:29 - 17:31
    set it to false and remember
  • 17:31 - 17:32
    false can is actually a keyword
  • 17:32 - 17:34
    in json's schema, tells us
  • 17:34 - 17:36
    that in our data we're not
  • 17:36 - 17:37
    allowed to have any properties
  • 17:37 - 17:40
    beyond those that are specified in the schema.
  • 17:40 - 17:41
    So now we validate and we
  • 17:41 - 17:43
    get an error, because the property
  • 17:43 - 17:46
    rating hasn't been defined in the schema.
  • 17:46 - 17:48
    If additional properties is missing,
  • 17:48 - 17:50
    or have the default value
  • 17:50 - 17:53
    of "true", then the validation goes through.
  • 17:53 - 17:56
    Now lets take a look at our authors that are still here.
  • 17:56 - 17:58
    Let's suppose that we don't
  • 17:58 - 18:01
    have a first name for our middle author here.
  • 18:01 - 18:02
    If we take that away and
  • 18:02 - 18:04
    we try to validate, we do
  • 18:04 - 18:06
    get an error, because we specified
  • 18:06 - 18:08
    in our schema and it's right
  • 18:08 - 18:11
    down here--that author-objects must
  • 18:11 - 18:14
    have both a first name and a last name.
  • 18:14 - 18:16
    It turns out that we can
  • 18:16 - 18:20
    specify for every property that the property is optional.
  • 18:20 - 18:21
    So, we can add to the
  • 18:21 - 18:23
    description of the first
  • 18:23 - 18:24
    name, not only that the
  • 18:24 - 18:26
    type is a string but that that
  • 18:26 - 18:27
    property is optional so we
  • 18:27 - 18:31
    say optional, true.
  • 18:31 - 18:34
    Now let's validate, and now we're in good shape.
  • 18:34 - 18:35
    Now, let's take a look
  • 18:35 - 18:36
    at what happens when we have
  • 18:36 - 18:37
    object that has more than
  • 18:37 - 18:41
    one instance of the same label or same property.
  • 18:41 - 18:43
    So let's suppose, for example, in
  • 18:43 - 18:45
    our magazine, the magazine
  • 18:45 - 18:46
    has two different years, 2009 and 2011.
  • 18:46 - 18:52
    This is syntactically valid, JSON,
  • 18:52 - 18:55
    it meets the structure of having a list of label-value pairs.
  • 18:55 - 18:57
    When we validate it, we
  • 18:57 - 19:00
    see that we can't add a second property, year.
  • 19:00 - 19:02
    So this validator doesn't permit
  • 19:02 - 19:04
    two copies of the same
  • 19:04 - 19:05
    property, and it's actually kind
  • 19:05 - 19:07
    of a parsing thing and not
  • 19:07 - 19:09
    so much related to JSON's schema.
  • 19:09 - 19:12
    Many parsers actually do enforce
  • 19:12 - 19:14
    that labels or properties need
  • 19:14 - 19:15
    to be unique within objects, even
  • 19:15 - 19:18
    though technically syntactically correct
  • 19:18 - 19:20
    JSON does allow multiple copies.
  • 19:20 - 19:22
    So that's just something to remember,
  • 19:22 - 19:23
    the typical use of objects is
  • 19:23 - 19:26
    to have unique labels, sometimes
  • 19:26 - 19:30
    are even called keys of which evokes a concept of them unique.
  • 19:30 - 19:32
    So typically they are unique.
  • 19:32 - 19:34
    They don't have to be for syntactic validity.
  • 19:34 - 19:35
    Usually when you wanna have
  • 19:35 - 19:39
    repeated values, it actually makes more sense to create an array.
  • 19:39 - 19:41
    I've taken away the second year in order to make the JSON valid again.
  • 19:41 - 19:44
    Now let's take a look at months.
  • 19:44 - 19:46
    I've used months to illustrate
  • 19:46 - 19:48
    the enumeration constraint so we
  • 19:48 - 19:50
    saw that we could constrain the
  • 19:50 - 19:52
    values of integers, and we
  • 19:52 - 19:54
    saw that we can constrain strings
  • 19:54 - 19:55
    using a pattern, but we can
  • 19:55 - 19:57
    also constrain any type by
  • 19:57 - 19:59
    enumerating the values that are allowed.
  • 19:59 - 20:00
    So, for the month, we've set
  • 20:00 - 20:02
    it a string type which it
  • 20:02 - 20:03
    is but we've further constrained it
  • 20:03 - 20:05
    by saying that string must be
  • 20:05 - 20:08
    either January or February.
  • 20:08 - 20:09
    So, if we try to say
  • 20:09 - 20:14
    put in the string March, we
  • 20:14 - 20:17
    validate and we get the obvious error here.
  • 20:17 - 20:18
    We can fix that by changing the
  • 20:18 - 20:19
    month back, but maybe it
  • 20:19 - 20:21
    makes more sense that March
  • 20:21 - 20:23
    would be part of our enumeration type,
  • 20:23 - 20:24
    so we'll add March to
  • 20:24 - 20:27
    the possible values for months, and now we're good.
  • 20:27 - 20:28
    As a next example, let's take
  • 20:28 - 20:30
    a look at something that we
  • 20:30 - 20:31
    saw was syntactically correct but
  • 20:31 - 20:33
    isn't going to be semantically
  • 20:33 - 20:34
    correct, which is when
  • 20:34 - 20:36
    we have the author list
  • 20:36 - 20:39
    be a mixture of objects and strings.
  • 20:39 - 20:43
    So, let's put Jeffrey Ullman here just as a string.
  • 20:43 - 20:44
    We saw that that was still
  • 20:44 - 20:46
    valid JSON, but when we
  • 20:46 - 20:47
    try to validate now, we're gonna
  • 20:47 - 20:49
    get an error because we expected
  • 20:49 - 20:50
    to see an object, we have
  • 20:50 - 20:52
    specified that the authors
  • 20:52 - 20:54
    are objects, and instead we got a string.
  • 20:54 - 20:56
    Now JSON schema does allow
  • 20:56 - 20:58
    us to specify that we
  • 20:58 - 21:00
    can have different types of data
  • 21:00 - 21:02
    in the same context, and I'm
  • 21:02 - 21:05
    going to show that with a little bit of a simpler example here.
  • 21:05 - 21:06
    So, let's first take away our
  • 21:06 - 21:09
    author there so that we're back with a valid file.
  • 21:09 - 21:13
    And what I am going to look at is simply the year values.
  • 21:13 - 21:15
    So, let suppose for whatever
  • 21:15 - 21:16
    reason that in our
  • 21:16 - 21:17
    magazines, one of the
  • 21:17 - 21:21
    years was a string and the other year was an integer.
  • 21:21 - 21:22
    So that's not gonna work out
  • 21:22 - 21:23
    right now because we have
  • 21:23 - 21:27
    specified clearly that the year must be an integer.
  • 21:27 - 21:29
    In JSON schema specifications, when we
  • 21:29 - 21:31
    want to allow multiple types
  • 21:31 - 21:32
    for values that are
  • 21:32 - 21:34
    used in the same context, we
  • 21:34 - 21:36
    actually make the type be an array.
  • 21:36 - 21:37
    So instead of just saying
  • 21:37 - 21:38
    integer, if we put
  • 21:38 - 21:40
    an array here that has
  • 21:40 - 21:42
    both integer and string that's
  • 21:42 - 21:43
    telling us that our year
  • 21:43 - 21:45
    value can be either an
  • 21:45 - 21:46
    integer or a string
  • 21:46 - 21:48
    and now when we validate,
  • 21:48 - 21:50
    we get a correct JSON file.
  • 21:50 - 21:53
    That concludes our demo of JSON schema validation.
  • 21:53 - 21:54
    Again, we've just seen
  • 21:54 - 21:56
    one example with a number
  • 21:56 - 21:58
    of the constructs that are available
  • 21:58 - 21:59
    in JSON schema, but it's not
  • 21:59 - 22:01
    nearly exhaustive, there are many
  • 22:01 - 22:02
    others, and I encourage you
  • 22:02 - 22:04
    to read a bit more about it.
  • 22:04 - 22:06
    You can download this data and
  • 22:06 - 22:07
    this schema as a starting
  • 22:07 - 22:09
    point, and start adding things playing around
  • 22:09 - 22:10
    and I think you'll get a
  • 22:10 - 22:12
    good feel for how JSON
  • 22:12 - 22:13
    schema can be used to
  • 22:13 -
    constrain the allowable data in a JSON file.
Title:
04-02-json-demo.mp4
Video Language:
English
Duration:
22:17
Amara Bot added a translation

English subtitles

Revisions