Return to Video

04-01-json-intro.mp4

  • 0:00 - 0:02
    This video introduces JSON.
  • 0:02 - 0:04
    Let's start by talking about its pronunciation.
  • 0:04 - 0:07
    Some people call it Jason, and some call it J-sahn.
  • 0:07 - 0:09
    I'll do a little bit of
  • 0:09 - 0:10
    investigation and discovered that the
  • 0:10 - 0:12
    original developer of JSON calls
  • 0:12 - 0:15
    it JSON so, I'll do that too.
  • 0:15 - 0:18
    Like XML, JSON can be thought of as a data model.
  • 0:18 - 0:20
    An alternative to the relational data
  • 0:20 - 0:21
    model that is more
  • 0:21 - 0:24
    appropriate for semi-structured data.
  • 0:24 - 0:26
    In this video I'll introduce the
  • 0:26 - 0:27
    basics of JSON and I'll
  • 0:27 - 0:29
    actually compare JSON to the
  • 0:29 - 0:32
    relational data model and I'll compare it to XML.
  • 0:32 - 0:34
    But it's not crucial to have
  • 0:34 - 0:37
    watched those videos to get something out of this one.
  • 0:37 - 0:38
    Now among the three models
  • 0:38 - 0:40
    - the relational model, XML, and
  • 0:40 - 0:41
    JSON - JSON is by
  • 0:41 - 0:43
    a large margin the newest,
  • 0:43 - 0:44
    and it does show there aren't
  • 0:44 - 0:46
    as many tools for JSON
  • 0:46 - 0:48
    as we have for XML and
  • 0:48 - 0:51
    certainly not as we have for relational.
  • 0:51 - 0:54
    JSON stands for Javascript object notation.
  • 0:54 - 0:56
    Although it's evolved to become pretty
  • 0:56 - 0:59
    much independent of Javascript at this point.
  • 0:59 - 1:03
    The little snippet of Jason in the corner right now mostly for decoration.
  • 1:03 - 1:05
    We'll talk about the details in just a minute.
  • 1:05 - 1:07
    Now JSON was designed
  • 1:07 - 1:09
    originally for what's called
  • 1:09 - 1:11
    serializing data objects.
  • 1:11 - 1:13
    That is taking the objects that
  • 1:13 - 1:14
    are in a program and sort
  • 1:14 - 1:15
    of writing them down in a
  • 1:15 - 1:18
    serial fashion, typically in files.
  • 1:18 - 1:19
    one thing about json
  • 1:19 - 1:21
    is that it is human readable,
  • 1:21 - 1:23
    similar to the way xml
  • 1:23 - 1:25
    is human readable and is
  • 1:25 - 1:27
    often use for data interchange.
  • 1:27 - 1:28
    So, for writing out, say
  • 1:28 - 1:30
    the objects program so that
  • 1:30 - 1:32
    they can be exchanged with another
  • 1:32 - 1:34
    program and read into that one.
  • 1:34 - 1:36
    Also, just more generally, because
  • 1:36 - 1:38
    json is not as rigid
  • 1:38 - 1:40
    as the relational model, it's generally
  • 1:40 - 1:42
    useful for representing and for
  • 1:42 - 1:43
    storing data that doesn't
  • 1:43 - 1:47
    have rigid structure that we've been calling semi-structured data.
  • 1:47 - 1:49
    As I mentioned json is
  • 1:49 - 1:51
    no longer closely tied to
  • 1:51 - 1:54
    Many different programming languages do
  • 1:54 - 1:56
    have parsers for reading json
  • 1:56 - 1:57
    data into the program and
  • 1:57 - 2:00
    for writing out json data as well.
  • 2:00 - 2:01
    Now, let's talk about the basic
  • 2:01 - 2:03
    constructs in JSON, and as
  • 2:03 - 2:06
    we will see this constructs are recursively defined.
  • 2:06 - 2:08
    We'll use the example JSON
  • 2:08 - 2:09
    data shown on the screen
  • 2:09 - 2:11
    and that data is also available
  • 2:11 - 2:14
    in a file for download from the website.
  • 2:14 - 2:18
    The basic atomic values in JSON are fairly typical.
  • 2:18 - 2:21
    We have numbers, we have strings.
  • 2:21 - 2:23
    We also have Boolean Values
  • 2:23 - 2:24
    although there are none of those
  • 2:24 - 2:29
    in this example, that's true and false, and no values.
  • 2:29 - 2:30
    There are two types of composite
  • 2:30 - 2:34
    values in JSON: objects and arrays.
  • 2:34 - 2:36
    Objects are enclosed in curly
  • 2:36 - 2:37
    braces and they consist
  • 2:37 - 2:40
    of sets of label-value pairs.
  • 2:40 - 2:41
    For example, we have an
  • 2:41 - 2:44
    object here that has a first name and a last name.
  • 2:44 - 2:46
    We have a more -
  • 2:46 - 2:48
    bigger, let's say, object here
  • 2:48 - 2:51
    that has ISBN, price, edition, and so on.
  • 2:51 - 2:53
    When we do our JSON demo,
  • 2:53 - 2:55
    we'll go into these constructs in more detail.
  • 2:55 - 2:57
    At this point, we're just introducing them.
  • 2:57 - 2:59
    the second type of composite
  • 2:59 - 3:01
    value in JSON is arrays,
  • 3:01 - 3:03
    and arrays are enclosed in square
  • 3:03 - 3:06
    brackets with commas between the array elements.
  • 3:06 - 3:07
    Actually we have commas in the objects
  • 3:07 - 3:10
    as and arrays are list of values.
  • 3:10 - 3:11
    For example, we can see
  • 3:11 - 3:13
    here that authors is a
  • 3:13 - 3:16
    list of author objects.
  • 3:16 - 3:18
    Now I mentioned that the constructs
  • 3:18 - 3:20
    are recursive, specifically the values
  • 3:20 - 3:22
    inside arrays can be anything,
  • 3:22 - 3:23
    they can be other arrays or objects,
  • 3:23 - 3:26
    space values and the values
  • 3:26 - 3:27
    are making up the label value
  • 3:27 - 3:29
    pairs and objects can also
  • 3:29 - 3:32
    be any composite value or a base value.
  • 3:32 - 3:33
    And I did want to
  • 3:33 - 3:34
    mention, by the way, that sometime
  • 3:34 - 3:36
    this word label here for
  • 3:36 - 3:39
    label value pairs is called a "property".
  • 3:39 - 3:41
    So, just like XML, JSON
  • 3:41 - 3:44
    has some basic structural requirements in
  • 3:44 - 3:45
    its format but it doesn't
  • 3:45 - 3:47
    have a lot of requirements in terms of uniformity.
  • 3:47 - 3:49
    We have a couple of examples
  • 3:49 - 3:51
    of heterogeneity in here, for
  • 3:51 - 3:52
    example, this book has an
  • 3:52 - 3:53
    edition and the other one
  • 3:53 - 3:57
    doesn't this book has a remark and the other one doesn't.
  • 3:57 - 3:59
    But we'll see many more examples
  • 3:59 - 4:00
    of heterogeneity when we do
  • 4:00 - 4:03
    the demo and look into JSON data in more detail.
  • 4:03 - 4:06
    Now let's compare JSON and the relational model.
  • 4:06 - 4:07
    We will see that many of
  • 4:07 - 4:09
    the comparisons are fairly similar
  • 4:09 - 4:12
    to when we compared XML to the relational model.
  • 4:12 - 4:15
    Let's start with the basic structures underling the data model.
  • 4:15 - 4:18
    So, the relational model is based on tables.
  • 4:18 - 4:20
    We set up structure of
  • 4:20 - 4:22
    table, a set of columns, and
  • 4:22 - 4:25
    then the data becomes rows in those tables.
  • 4:25 - 4:27
    JSON is based instead on
  • 4:27 - 4:29
    sets, the sets of label
  • 4:29 - 4:34
    pairs and arrays and as we saw, they can be nested.
  • 4:34 - 4:35
    One of the big differences between
  • 4:35 - 4:38
    the two models, of course, is the scheme.
  • 4:38 - 4:39
    So the Relational model has a
  • 4:39 - 4:41
    Schema fixed in advance,
  • 4:41 - 4:43
    you set it up before you
  • 4:43 - 4:44
    have any data loaded and then
  • 4:44 - 4:47
    all data needs to confirm to that Schema.
  • 4:47 - 4:48
    Jason on the other other
  • 4:48 - 4:52
    hand typically does not require a schema in advance.
  • 4:52 - 4:53
    In fact, the schema and the
  • 4:53 - 4:55
    data are kinda mix together
  • 4:55 - 4:56
    just like an xml, and
  • 4:56 - 4:58
    this is often referred to as
  • 4:58 - 5:00
    self-describing data, where the
  • 5:00 - 5:04
    schema elements are within the data itself.
  • 5:04 - 5:05
    And this is of course typically
  • 5:05 - 5:08
    more flexible than the to a model.
  • 5:08 - 5:10
    But there are advantages to having schema [sp?]
  • 5:10 - 5:12
    as well, definitely.
  • 5:12 - 5:13
    As far as queries go, one
  • 5:13 - 5:15
    of the nice features of the
  • 5:15 - 5:16
    relational model is that there
  • 5:16 - 5:21
    are simple, expressive languages for clearing the database.
  • 5:21 - 5:23
    In terms of json, although a
  • 5:23 - 5:25
    few New things have been proposed;
  • 5:25 - 5:27
    at this point there's nothing widely
  • 5:27 - 5:29
    used for querying Jason data.
  • 5:29 - 5:31
    Typically Jason data is
  • 5:31 - 5:34
    read into a program and it's manipulated programatically.
  • 5:34 - 5:35
    Now let me interject that this
  • 5:35 - 5:38
    video is being made in February 2012.
  • 5:38 - 5:40
    So it is possible
  • 5:40 - 5:42
    that some json query languages
  • 5:42 - 5:44
    will emerge and become
  • 5:44 - 5:45
    widely used there is just
  • 5:45 - 5:46
    nothing used at this point.
  • 5:46 - 5:47
    There are some proposals.
  • 5:47 - 5:49
    There's a JSON path language,
  • 5:49 - 5:52
    JSON Query, a language called jaql.
  • 5:52 - 5:53
    It may be that just like
  • 5:53 - 5:55
    XML, the query language are
  • 5:55 - 5:57
    gonna follow the prevalent use
  • 5:57 - 5:59
    of the data format or the data model.
  • 5:59 - 6:01
    But that does not happened yet, as of February 2012.
  • 6:01 - 6:04
    How about ordering?
  • 6:04 - 6:07
    One aspect of the relational model is that it's an unordered model.
  • 6:07 - 6:08
    It's based on sets and
  • 6:08 - 6:10
    if we want to see relational
  • 6:10 - 6:14
    data in sorted order then we put that inside a query.
  • 6:14 - 6:16
    In JSON, we have arrays as
  • 6:16 - 6:19
    one of the basic data structures, and arrays are ordered.
  • 6:19 - 6:20
    Of course, there's also the fact like
  • 6:20 - 6:22
    XML that JSON data is
  • 6:22 - 6:24
    often is usually written files
  • 6:24 - 6:26
    and files themselves are naturally ordered,
  • 6:26 - 6:27
    but the ordering of the data
  • 6:27 - 6:30
    in files usually isn't relevant,
  • 6:30 - 6:31
    sometimes it is, but
  • 6:31 - 6:33
    typically not finally in
  • 6:33 - 6:35
    terms of implementation, for the
  • 6:35 - 6:37
    relational model, there are
  • 6:37 - 6:39
    systems that implement the relational model natively.
  • 6:39 - 6:42
    They're very generally quite
  • 6:42 - 6:44
    efficient and powerful systems.
  • 6:44 - 6:46
    For json, we haven't yet
  • 6:46 - 6:48
    seen stand alone database systems
  • 6:48 - 6:49
    that use json their data
  • 6:49 - 6:51
    model instead JSON is
  • 6:51 - 6:54
    more typically coupled with programming languages.
  • 6:54 - 6:56
    One thing I should add however
  • 6:56 - 7:00
    JSON is used in NoSQL systems.
  • 7:00 - 7:02
    We do have videos about NoSQL
  • 7:02 - 7:05
    systems you may or may not have, have watched those yet.
  • 7:05 - 7:08
    There's a couple of different ways that JSON is used used in those systems.
  • 7:08 - 7:10
    One of them is just as
  • 7:10 - 7:11
    a format for reading data
  • 7:11 - 7:14
    into the systems and writing data out from the systems.
  • 7:14 - 7:15
    The other way that it is
  • 7:15 - 7:17
    used is that some of the
  • 7:17 - 7:18
    note systems are what are
  • 7:18 - 7:20
    called "Document Management Systems" where
  • 7:20 - 7:22
    the documents themselves may contain
  • 7:22 - 7:24
    JSON data and then the systems
  • 7:24 - 7:26
    will have special features for manipulating
  • 7:26 - 7:29
    the JSON in the document is better stored by the system.
  • 7:29 - 7:32
    Now let's compared json and XML.
  • 7:32 - 7:35
    This is actually a hotly debated comparison right now.
  • 7:35 - 7:37
    There are signification overlap in
  • 7:37 - 7:40
    the usage of JSON and XML.
  • 7:40 - 7:41
    Both of them are very
  • 7:41 - 7:43
    good for putting semi-structured data
  • 7:43 - 7:46
    into a file format
  • 7:46 - 7:48
    and using it for data interchange.
  • 7:48 - 7:49
    And so because there's so
  • 7:49 - 7:50
    much overlap in what they're used
  • 7:50 - 7:54
    for, it's not surprising that there's significant debate.
  • 7:54 - 7:55
    I'm not gonna take sides.
  • 7:55 - 7:57
    I'm just going to try to give you a comparison.
  • 7:57 - 7:58
    Let's start by looking at the
  • 7:58 - 8:02
    verbosity of expressing data in the two languages.
  • 8:02 - 8:03
    So it is the case
  • 8:03 - 8:05
    that XML is in general,
  • 8:05 - 8:08
    a little more verbose than Jason.
  • 8:08 - 8:09
    So the same data expressed in
  • 8:09 - 8:11
    the 2 formats will tend to
  • 8:11 - 8:12
    have more characters [xx] than Json
  • 8:12 - 8:14
    and you can see that
  • 8:14 - 8:16
    in our examples because our big
  • 8:16 - 8:18
    Json example was actually pretty
  • 8:18 - 8:20
    much the same data that we used when we showed XML.
  • 8:20 - 8:22
    And the reason for
  • 8:22 - 8:23
    XML being a bit more
  • 8:23 - 8:24
    verbose largely has to
  • 8:24 - 8:26
    do actually with closing tags,
  • 8:26 - 8:29
    and some other features.
  • 8:29 - 8:30
    But I'll let you judge
  • 8:30 - 8:32
    for yourself whether the somewhat
  • 8:32 - 8:35
    longer expression of XML is a problem.
  • 8:35 - 8:37
    Second is complexity, and here,
  • 8:37 - 8:39
    too, most people would say
  • 8:39 - 8:42
    that XML is a bit more complex than JSON.
  • 8:42 - 8:45
    I'm not sure I entirely agree with that comparison.
  • 8:45 - 8:47
    If you look at the subset
  • 8:47 - 8:49
    of XML that people really
  • 8:49 - 8:51
    use, you've got attributes,
  • 8:51 - 8:52
    sub elements and text, and
  • 8:52 - 8:54
    that's more or less it.
  • 8:54 - 8:55
    If you look at Json, you got
  • 8:55 - 8:58
    your basic values and you've got your objects and your arrays.
  • 8:58 - 8:59
    I think the issue is that
  • 8:59 - 9:01
    XML has a lot of
  • 9:01 - 9:03
    extra stuff that goes along with it.
  • 9:03 - 9:06
    So if you read the entire XML specification.
  • 9:06 - 9:08
    It will take you a long time.
  • 9:08 - 9:10
    JSON, you can grasp the
  • 9:10 - 9:12
    entire specification a little bit more quickly.
  • 9:12 - 9:14
    Now let's turn to validity.
  • 9:14 - 9:16
    And by validity, I mean the
  • 9:16 - 9:18
    ability to specify constraints or
  • 9:18 - 9:20
    restriction or schema on
  • 9:20 - 9:22
    the structure of data
  • 9:22 - 9:24
    in one of these models, and
  • 9:24 - 9:27
    have it enforced by tools or by a system.
  • 9:27 - 9:28
    Specifically in XML we
  • 9:28 - 9:30
    have the notion of document type
  • 9:30 - 9:32
    descriptors, or DTDs, we also
  • 9:32 - 9:34
    have XML Schema which
  • 9:34 - 9:38
    gives us XSD's, XML Schema Descriptors.
  • 9:38 - 9:39
    And these are schema like
  • 9:39 - 9:41
    things that we can specify, and
  • 9:41 - 9:42
    we can have our data checked to
  • 9:42 - 9:43
    make sure it conforms to the
  • 9:43 - 9:45
    schema, and these are, I would say,
  • 9:45 - 9:49
    fairly widely used at this point for XML.
  • 9:49 - 9:51
    For JSON, there's something called JSON Schema.
  • 9:51 - 9:53
    And, you know, similar to
  • 9:53 - 9:55
    XML Schema, it's a way
  • 9:55 - 9:57
    to specify the structure and then
  • 9:57 - 9:58
    we can check that JSON conforms
  • 9:58 - 10:02
    that and we will see some of that in our demo.
  • 10:02 - 10:04
    The current status, February
  • 10:04 - 10:07
    2012 is that this is
  • 10:07 - 10:09
    not widely used this point.
  • 10:09 - 10:11
    But again, it could really just be evolution.
  • 10:11 - 10:14
    If we look back
  • 10:14 - 10:15
    at XML, as it was originally
  • 10:15 - 10:17
    proposed, probably we didn't
  • 10:17 - 10:18
    see a whole of lot of use
  • 10:18 - 10:20
    of DTDs, and in fact not
  • 10:20 - 10:22
    as XSDs for sure until later on.
  • 10:22 - 10:26
    So we'll just have to see whether JSON evolves in a similar way.
  • 10:26 - 10:31
    Now the programming interface is where JSON really shines.
  • 10:31 - 10:34
    The programming interface for XML can be fairly clunky.
  • 10:34 - 10:37
    The XML model, the attributes
  • 10:37 - 10:39
    and sub-elements and so on,
  • 10:39 - 10:41
    don't typically match the model
  • 10:41 - 10:43
    of data inside a programming language.
  • 10:43 - 10:47
    In fact, that's something called the impedance mismatch.
  • 10:47 - 10:48
    The impedance miss match
  • 10:48 - 10:50
    has been discussed in database
  • 10:50 - 10:52
    systems actually, for decades
  • 10:52 - 10:54
    because one of the original
  • 10:54 - 10:56
    criticisms of relational database
  • 10:56 - 10:57
    systems is that the data
  • 10:57 - 10:59
    structures used in the database,
  • 10:59 - 11:01
    specifically tables, didn't match
  • 11:01 - 11:04
    directly with the data structures and programming languages.
  • 11:04 - 11:05
    So there had to be some manipulation
  • 11:05 - 11:09
    at the interface between programming languages and the database system and that's the mismatch.
  • 11:09 - 11:13
    So that same impedance mismatch
  • 11:13 - 11:15
    is pretty much present
  • 11:15 - 11:17
    in XML wherein JSON is
  • 11:17 - 11:19
    really a more direct mapping
  • 11:19 - 11:23
    between many programming languages and the structures of JSON.
  • 11:23 - 11:25
    Finally, let's talk about querying.
  • 11:25 - 11:27
    I've already touched on this
  • 11:27 - 11:28
    a bit, but JSON does not
  • 11:28 - 11:31
    have any mature, widely
  • 11:31 - 11:33
    used query languages at this point.
  • 11:33 - 11:34
    for XML we do have
  • 11:34 - 11:36
    XPath, we have XQuery,
  • 11:36 - 11:39
    we have XSLT.
  • 11:39 - 11:41
    Maybe not all of
  • 11:41 - 11:42
    them are widely used but there's
  • 11:42 - 11:44
    no question that XPath at least and
  • 11:44 - 11:46
    XSL are used quiet a bit.
  • 11:46 - 11:48
    As far as Json goes there
  • 11:48 - 11:50
    is a proposal called Json path.
  • 11:50 - 11:52
    It looks actually quiet a lot
  • 11:52 - 11:55
    like XPath maybe he'll catch on.
  • 11:55 - 11:56
    There's something called JSON Query.
  • 11:56 - 11:58
    Doesn't look so much like
  • 11:58 - 12:01
    XML Query, I mean, XQuery.
  • 12:01 - 12:02
    and finally, there has been a
  • 12:02 - 12:07
    proposal called [xx] language, but
  • 12:07 - 12:08
    again as of February 2012
  • 12:08 - 12:10
    all of these are still very
  • 12:10 - 12:13
    early, so we just don't know what's going to catch on.
  • 12:13 - 12:16
    So now let's talk about the validity of JSON data.
  • 12:16 - 12:17
    So do JSON data that's
  • 12:17 - 12:19
    syntacti[xx] valid, simply needs
  • 12:19 - 12:22
    to adhere to the basic structural requirements.
  • 12:22 - 12:24
    As a reminder, that would be
  • 12:24 - 12:25
    that we have sets of label
  • 12:25 - 12:27
    value pairs, we have arrays
  • 12:27 - 12:29
    of values and our values
  • 12:29 - 12:31
    are from predefined types.
  • 12:31 - 12:34
    And again, these values here are defined recursively.
  • 12:34 - 12:35
    So we start with a JSON
  • 12:35 - 12:37
    file and we send
  • 12:37 - 12:39
    it to a the parser
  • 12:39 - 12:40
    may determine that the file
  • 12:40 - 12:42
    has syntactic errors or if
  • 12:42 - 12:44
    the file is syntactically correct then
  • 12:44 - 12:47
    it can parsed into objects in a programming language.
  • 12:47 - 12:49
    Now if we're interested in semantically
  • 12:49 - 12:51
    valid JSON; that is
  • 12:51 - 12:52
    JSON that conforms to
  • 12:52 - 12:54
    some constraints or a schema,
  • 12:54 - 12:55
    then in addition to checking the
  • 12:55 - 12:57
    basics structural requirements, we check
  • 12:57 - 13:00
    whether JSON conforms to the specified schema.
  • 13:00 - 13:02
    If we use a language like JSON
  • 13:02 - 13:03
    schema for example, we put
  • 13:03 - 13:05
    a specification in as a
  • 13:05 - 13:07
    separate file, and in
  • 13:07 - 13:09
    fact JSON schema is expressed in
  • 13:09 - 13:11
    JSON itself, as we'll see
  • 13:11 - 13:12
    in our demo, we send it
  • 13:12 - 13:13
    to a validator and that
  • 13:13 - 13:15
    validator might find that there
  • 13:15 - 13:16
    are some syntactic errors or
  • 13:16 - 13:17
    it may find that there are
  • 13:17 - 13:19
    some symantic errors so the
  • 13:19 - 13:21
    data could to be correct syntactically
  • 13:21 - 13:23
    but not conform to the schema.
  • 13:23 - 13:25
    If it's both syntactically and semantically
  • 13:25 - 13:26
    correct then it can move
  • 13:26 - 13:28
    on to the parser where
  • 13:28 - 13:30
    will be parsed again into
  • 13:30 - 13:32
    objects in a programming language.
  • 13:32 - 13:36
    So to summarize, JSON stands for Java Script Object Notation.
  • 13:36 - 13:38
    It's a standard for taking data
  • 13:38 - 13:41
    objects and serializing them into a format that's human readable.
  • 13:41 - 13:43
    It's also very useful for
  • 13:43 - 13:46
    exchanging data between programs,
  • 13:46 - 13:48
    and for representing and storing
  • 13:48 - 13:51
    semi-structured data in a flexible fashion.
  • 13:51 - 13:52
    In the next video we'll go
  • 13:52 - 13:55
    live with a demonstration of JSON.
  • 13:55 - 13:56
    We'll use a couple of JSON
  • 13:56 - 13:57
    editors, we'll take a
  • 13:57 - 13:59
    look at the structure of JSON
  • 13:59 - 14:01
    data, when it's syntactically correct.
  • 14:01 - 14:03
    We'll demonstrate how it's very
  • 14:03 - 14:05
    flexible when our data might
  • 14:05 - 14:06
    irregular, and we'll also
  • 14:06 - 14:09
    demonstrate schema checking using
  • 14:09 -
    an example of JSON's schema.
Title:
04-01-json-intro.mp4
Duration:
14:12
Amara Bot added a translation

English subtitles

Revisions