< Return to Video

8.3: What is JSON? Part II - p5.js Tutorial

  • 0:00 - 0:02
    Hello. So I'm now in Part 2
  • 0:02 - 0:06
    of this discussion about working with JSON data
  • 0:06 - 0:08
    In the previous video I looked at
  • 0:08 - 0:12
    this wonderful thing called a Sunflower
    A beautiful flower.
  • 0:12 - 0:16
    And how you might encapsulate the idea
    of a sunflower
  • 0:16 - 0:17
    as data in a JSON file.
  • 0:17 - 0:19
    It was kind of a reasonable demonstration
  • 0:19 - 0:22
    for this first step of working with data.
  • 0:22 - 0:25
    But if you look at it
    it's real simple.
  • 0:25 - 0:28
    It's a single object.
    It's got 4 properties in it.
  • 0:28 - 0:31
    There's not a lot of complexity.
    Flower.Name ; Flower.R
  • 0:31 - 0:32
    That's all you sorta had to worry about.
  • 0:32 - 0:37
    But realistically your going to find a
    Data Set that's going to have a lot more in it.
  • 0:37 - 0:38
    You might want to have a Data Set
  • 0:38 - 0:44
    that has the high temperature in Tokyo
    everyday for the last year
  • 0:44 - 0:48
    and lots of other information about the
    the weather that day.
  • 0:48 - 0:51
    And there's going to be a list, and
    a whole bunch of nested objects.
  • 0:51 - 0:53
    So what's a scenario like that
    and how do you deal with it?
  • 0:53 - 0:59
    So the scenario I'm going to use I think;
    I'll pull it from this website I mentioned
  • 0:59 - 1:01
    that has a whole lot of data sources
    in it that you can grab.
  • 1:01 - 1:04
    Um, Let's think about Birds for a second.
  • 1:04 - 1:06
    Another personal favorite topic of mine.
  • 1:06 - 1:10
    I'm not going to be able to pull things
    about birds out of a hat; but,
  • 1:10 - 1:17
    First things first. If you go back to this
    there's just a single thing in there.
  • 1:17 - 1:20
    I really just should keep going with
    the flowers thing.
  • 1:20 - 1:21
    But the example I have is Birds.
  • 1:21 - 1:26
    So what if your data isn't like a
    single object but is actually
  • 1:26 - 1:29
    a whole bunch of objects. Like here are
    10 images from Google in a church.
  • 1:29 - 1:32
    Or here are 10 recent articles
    from the New York Times.
  • 1:32 - 1:38
    What if your data is 10 Birds that live
    in Antarctica. Is that what I have?
  • 1:38 - 1:40
    I can't remember
    if that's the data I have.
  • 1:40 - 1:48
    So the Data Set; Again a JavaScript
    starts with a Curly bracket '{'
  • 1:48 - 1:51
    and a Closed Curly bracket '}'
    To indicate it's and Object.
  • 1:51 - 1:55
    But the data itself might have
    an Array in it.
  • 1:55 - 2:04
    And that array might be named Birds.
  • 2:04 - 2:10
    And each one of those birds might be an Object
  • 2:10 - 2:18
    with a Name: Penguin and a Height
  • 2:18 - 2:21
    (awww Can't think of a size)
  • 2:24 - 2:29
    Let's just go with Height. I don't know.
    How tall are penguins?
    They're small. 1 Meter?
  • 2:29 - 2:33
    Alright then, That's an Object.
    And then the next thing in the array
  • 2:33 - 2:38
    is the name, an Eagle.
  • 2:38 - 2:41
    These should be in quotes.
  • 2:44 - 2:50
    And the height of an eagle is
    about 3 meters.
  • 2:50 - 2:52
    Obviously I don't know anything
    about birds today we are finding out.
  • 2:52 - 2:59
    So you can see how this now has a bit
    more complexity than just a sunflower.
  • 2:59 - 3:03
    Because, what we have here is just
    an Object that has a property in it
  • 3:03 - 3:06
    called Birds. That property is an array.
  • 3:06 - 3:09
    Each element of the array is an Object.
  • 3:09 - 3:16
    So let's say for example: We loaded this
    into a Variable called DATA.
  • 3:16 - 3:23
    How would you get access to the
    Height of the Eagle for example.
  • 3:24 - 3:30
    data (dot) birds would get me here.
  • 3:30 - 3:36
    Now this is an array. This is element [0].
    This is element [1].
  • 3:36 - 3:39
    So if it's an array I've gotta say index [1].
  • 3:39 - 3:41
    So now I'm in this object.
  • 3:41 - 3:46
    And I want to get the Height.
    (dot) height
  • 3:46 - 3:50
    So you can see I have to figure out
    the Path in to this JSON file.
  • 3:50 - 3:53
    I need to know the property name
    I'm looking for.
  • 3:53 - 3:57
    And if that property is an array, I need
    to know the index that I'm looking for.
  • 3:57 - 4:00
    And if that thing in the array is an Object
  • 4:00 - 4:02
    I need to know the property of that
    object I'm looking for.
  • 4:02 - 4:05
    So this is just one scenario.
  • 4:05 - 4:07
    I could just... I could probably
    like let this camera record
  • 4:07 - 4:11
    for like the next 72 hours and just keep
    making scenarios...
  • 4:11 - 4:15
    Like... We all become insane...
    In our world of JSON scenarios.
  • 4:15 - 4:19
    But, So, This is the kind of thing you really
    need to unfortunately... or fortunately
  • 4:19 - 4:22
    practice on your own as you start to find
    different data sources.
  • 4:22 - 4:25
    But let's do this one step further.
  • 4:25 - 4:28
    And I'm going to go pull
    an actual data set about Birds.
  • 4:28 - 4:31
    And we'll double back and do this
    again with that data set.
  • 4:31 - 4:33
    And there's actually another
    missing piece here.
  • 4:33 - 4:37
    What if you wanted to actually
    loop thru and do something;
  • 4:37 - 4:39
    If there are a hundred birds in this file,
  • 4:39 - 4:42
    How would I make this Index into
    the Array?
  • 4:42 - 4:44
    Some sort of variable like 'I'
  • 4:44 - 4:48
    So I could say.. What's the first bird,
    and the second bird, and the third bird?
  • 4:48 - 4:50
    So that's another aspect of this
    that's important.
  • 4:50 - 4:55
    Ok. So let's go to the... place where
    I'm going to find the data set.
  • 4:55 - 4:58
    It's the github repository.
  • 4:58 - 5:01
    And I'm going to skip talking about
    what github or git is.
  • 5:01 - 5:07
    But you can find the url. I'll post it somewhere.
    If you want to pause the video it's right up here.
  • 5:07 - 5:10
    Just '/corpora ' is all you will need.
    Ooops I'm already there.
  • 5:10 - 5:13
    So I want to go here. And I'm going to
    click under 'data'.
  • 5:13 - 5:16
    And I'm going to look for animals.
  • 5:16 - 5:19
    And I'm really like...
    Which one was I doing??
  • 5:19 - 5:22
    Birds of North America?? Hold On.
    Let's do the Antarctica.
  • 5:22 - 5:23
    'birds_antarctica.json'
  • 5:23 - 5:26
    So you can see here is the JSON file.
  • 5:26 - 5:28
    Now let's make this.. Actually..
    So let me... I'm Sorry...
  • 5:28 - 5:34
    Let me just do something to
    Copy it into.. 'P5'
  • 5:34 - 5:38
    So I'm going to make a new JSON file
    called 'birds.json'
  • 5:38 - 5:40
    I'm going to paste it in here.
  • 5:40 - 5:43
    I'm going to hit Save.
    I'm going to make this a little bigger.
  • 5:43 - 5:45
    And I'm going to come look at it here.
  • 5:45 - 5:47
    So here we have this scenario now.
  • 5:47 - 5:50
    And we have to do some detective work.
  • 5:50 - 5:52
    So let's come back over here.
  • 5:52 - 5:53
    And we're going to have to go
    back and forth a little bit.
  • 5:53 - 5:56
    This is no longer our JSON file.
  • 5:56 - 6:00
    Our JSON file is actually
  • 6:00 - 6:02
    If you can look at it a second
    while I erase.
  • 6:02 - 6:03
    It's that thing.
  • 6:03 - 6:04
    Take a look at it.
  • 6:04 - 6:06
    Does it make sense to you?
    What's the first property?
  • 6:06 - 6:08
    Is the first property an Array?
  • 6:08 - 6:11
    No it's not an array? Is it and array?
    I have to come over here and look...
  • 6:11 - 6:14
    The first property is Description.
  • 6:14 - 6:16
    Description is just some text.
  • 6:16 - 6:18
    The next property is Source.
    Which is also just some text.
  • 6:18 - 6:21
    The next property is Birds
    which is an array.
  • 6:21 - 6:24
    Each element of the array is an object
    that has 2 properties
  • 6:24 - 6:28
    One is "family".. Oops "members"
    And Members is also an array.
  • 6:28 - 6:30
    So you can see how nested this can become.
  • 6:30 - 6:32
    So let's say what I want to do...
  • 6:32 - 6:35
    This is like.. You wouldn't...
    This is sorta arbitrary..
  • 6:35 - 6:37
    You wouldn't necessarily want do this...
  • 6:37 - 6:40
    But, just to like make the, sorta case here.
  • 6:40 - 6:45
    I'm really interested in... I want to
    pull out exactly the imperial shag.
  • 6:45 - 6:53
    So how do I get to.. Actually let me not
    use that as an example...
  • 6:53 - 6:56
    Let's look at the.. I don't know
    how to pronounce this...
  • 6:56 - 6:57
    The ""Crozet shag"
  • 6:57 - 6:59
    I kinda want to do a Google search
    to see what that bird looks like.
  • 6:59 - 7:00
    Oh, "Cormorants".
  • 7:00 - 7:04
    I love Cormorants. They're good.
    They like fly over the water...
  • 7:04 - 7:08
    So... how do I get to this
    particular piece of data.
  • 7:08 - 7:11
    Let's say I have an Object called 'data'.
  • 7:11 - 7:13
    What's the first thing... This is embedded
  • 7:13 - 7:15
    in "members", which is in this object
  • 7:15 - 7:18
    which is in this other array
    called Birds.
  • 7:18 - 7:21
    So what I need to do is say...
  • 7:21 - 7:27
    data (dot) birds.
    That's the first thing I need to do.
  • 7:27 - 7:29
    Come back over here.
  • 7:29 - 7:30
    Now I'm in here.
  • 7:30 - 7:31
    I'm at "birds".
  • 7:31 - 7:35
    This whole object is Object[0].
  • 7:35 - 7:36
    This second object is Object[1].
  • 7:36 - 7:41
    So I want to be in Object[1]
  • 7:41 - 7:44
    What's the property I want to be
    in Object[1]?
  • 7:44 - 7:47
    Not "family". I want "members".
  • 7:47 - 7:55
    So Object[1] (dot) members.
  • 7:55 - 8:00
    And now, over here.
    Zero.. One.. Two...
  • 8:00 - 8:05
    So that's also an array. And I want to
    look at the "Crozet shag".
  • 8:05 - 8:09
    Which is the third element.
    Index[2] in that array.
  • 8:09 - 8:12
    So now over here.. Index[2].
  • 8:12 - 8:17
    Index[2]. So again, Searching through
    a JSON file to pull out
  • 8:17 - 8:22
    some aspect of the data is all about
    knowing the path.
  • 8:22 - 8:26
    What's the route element? What's the
    object inside of that you are looking at.
  • 8:26 - 8:31
    Is that object an array? If it is;
    What the element of that array
  • 8:31 - 8:34
    If that's an an object. Then you want to
    get the property of that object.
  • 8:34 - 8:36
    Which happens to be an array.
  • 8:36 - 8:38
    So; again, every scenario is
    going to be different.
  • 8:38 - 8:40
    But this is one scenario that's a bit
    more complex then just
  • 8:40 - 8:42
    that single sunflower example.
  • 8:42 - 8:46
    So now let's actually display this in our code.
  • 8:46 - 8:48
    To make sure... And I'm going to close the flower one.
  • 8:48 - 8:54
    And I'm going to say.. birds. I'm
    going to load that JSON file into here.
  • 8:54 - 8:56
    I'm just going to say 'createP'
  • 8:56 - 8:59
    Lets not even use a canvas.
  • 8:59 - 9:03
    If you haven't watched the videos on how
    to make a DOM element.
  • 9:03 - 9:07
    How to make paragraphs, and links,
    and other things on the page
  • 9:07 - 9:08
    you can look at that.
  • 9:08 - 9:14
    I'm going to say.
    'var bird = birds'
  • 9:14 - 9:19
    No, No, Data... I'm sorry...
    I want to make this Data
  • 9:19 - 9:23
    Are you looking at that?
    data (dot) birds
  • 9:23 - 9:28
    What did I say?
    Index[1] (dot) members index[2]
  • 9:28 - 9:31
    data.birds[1].members[2];
  • 9:31 - 9:35
    And now.. createP() that bird
  • 9:35 - 9:38
    And if we run this.. We're going to see...
    Look at that. There it is.
  • 9:38 - 9:40
    That's the bird I'm seeing on the screen.
  • 9:40 - 9:44
    Now again, this is like a trivial...
    Sort of like I'm proving that JSON works.
  • 9:44 - 9:47
    There's data in there. It has a structure.
  • 9:47 - 9:49
    That structure can be accessed.
  • 9:49 - 9:51
    But most likely what you;ll want to
    be doing is displaying everything
  • 9:51 - 9:53
    in the data set. Or
  • 9:53 - 9:56
    I'll create some tool that the user could
    search into the data set.
  • 9:56 - 9:59
    And ideally that set might have
    like thousands of birds in it.
  • 9:59 - 10:01
    Instead of just like 10 or how
    ever many are in there.
  • 10:01 - 10:04
    So let's go a little bit deeper.
  • 10:04 - 10:08
    I'm at 10 Minutes if you're
    sticking with me. I'm thankful again.
  • 10:08 - 10:12
    Cause I feel like.. I'm talking..
    It's weird to do this by myself...
  • 10:12 - 10:14
    In this room... even though atleast
    I have a window.
  • 10:14 - 10:16
    And I can see they're people on the street.
    Hello out there...
  • 10:16 - 10:20
    I'm making a video about JSON.
    Do you care??
  • 10:20 - 10:25
    I wonder if they're watching on their
    phone. Any back to this thing...
  • 10:25 - 10:29
    So, what if I wanted to do..
    Let's look at the data again.
  • 10:29 - 10:31
    I haven't done any singing yet...
  • 10:31 - 10:33
    which I probably should do to make this
    video more interesting.
  • 10:33 - 10:38
    What if what I wanted to do was display...
    I have an idea..
  • 10:38 - 10:40
    I think I did this as an example...
  • 10:40 - 10:46
    What I want to do is display the family...
    I want to display all the birds in this file.
  • 10:46 - 10:49
    And I want to see the Family as like..
    A Header.
  • 10:49 - 10:52
    And I want to see the Birds
    underneath it.
  • 10:52 - 10:55
    So like a bigger text for the family.
    And a smaller text for these.
  • 10:55 - 10:58
    So let's just start with that.
  • 10:58 - 11:02
    So the difference here is instead of
    looking for a particular item,
  • 11:02 - 11:07
    what I want to do... And it might
    make sense for me to say...
  • 11:07 - 11:10
    'var birds = data.birds'
  • 11:10 - 11:13
    So I'm just going to pull out that
    birds object in the file,
  • 11:13 - 11:16
    which is data (dot) birds.
    And stick it in a variable.
  • 11:16 - 11:19
    Because that's an array.
    And now I can say.
  • 11:19 - 11:22
    Loop thru birds (dot) length.
  • 11:22 - 11:24
    And let's just start by saying...
  • 11:24 - 11:30
    createElement('h1')
  • 11:30 - 11:35
    h1... Element ... h1... What??
  • 11:35 - 11:39
    Birds Index[i] (dot) Family
  • 11:39 - 11:42
    So I want birds at [0] (dot) family
    and index [1] (dot) family
  • 11:42 - 11:45
    and presumably there's more
    birds down here.
  • 11:45 - 11:49
    So.. birds (index) [i] (dot) family.
  • 11:49 - 12:00
    So this now... You can see...
    Ooops... I don't need this any more.
  • 12:00 - 12:02
    This was from my previous example.
  • 12:02 - 12:06
    You can see here, this is a way
    of how looping thru data.
  • 12:06 - 12:10
    So you can see that this particular loop
    is going thru and accessing
  • 12:10 - 12:16
    every element of the array, Birds
    and looking for the Family.
  • 12:16 - 12:18
    So these are just the family names.
  • 12:18 - 12:21
    But what I want to do is actually look
    thru all the members of the family names.
  • 12:21 - 12:26
    So inside that loop, each and every bird
    I need another loop to say
  • 12:26 - 12:29
    Each and every Member of
    each and every bird.
  • 12:29 - 12:30
    So how do I do that.
  • 12:30 - 12:37
    So first let me a variable called Members
    which is birds.members right
  • 12:37 - 12:43
    members, birds.members.
    That's that array and this array.
  • 12:43 - 12:50
    So now what I want to do is a for j;
    (because I need a different variably then I)
  • 12:50 - 12:54
    j < members.length
    j++
  • 12:54 - 13:00
    And I'll just say createDiv(members (index) [ j ]
  • 13:00 - 13:05
    Because; first, I want to get...
    I have all these bird objects.
  • 13:05 - 13:10
    And I want to just see the family name.
    Then I want to look at the members.
  • 13:10 - 13:13
    But the members is an array so I need to
    loop thru that array.
  • 13:13 - 13:15
    And let's see if that works.
  • 13:15 - 13:19
    Awww... Can not find...
    So what did I get wrong??
  • 13:19 - 13:26
    birds.members, var members...
    Let's look at this again
  • 13:26 - 13:31
    Aha... Its not birds (dot) members.
    It's what?
  • 13:31 - 13:35
    birds (index) [i] (dot) members.
    Classic mistake.
  • 13:35 - 13:38
    I'm looping thru an array called Birds
  • 13:38 - 13:41
    and I want to get the members of
    each element of the array.
  • 13:41 - 13:44
    So I want to get the members of
    element [0] and element [1]
  • 13:44 - 13:46
    That's what I did with Family.
  • 13:46 - 13:50
    So I need to have (index) [i] (dot) members
  • 13:50 - 13:52
    And now we can see.. Here look...
  • 13:52 - 13:56
    There are the Albatrosses, the Cormorants,
    the Diving Petrels, the Ducks, Geese and Swans,
  • 13:56 - 13:59
    the Gulls, the Penguins...
    Oooh lots of good Penguins...
  • 13:59 - 14:03
    This is a great data set. See now I have all
    of this data appearing on the page.
  • 14:03 - 14:07
    Now Again, I haisven't done anything that
    I would say was interesting with this data.
  • 14:07 - 14:11
    But I'm showing you just how to
    navigate a JSON file.
  • 14:11 - 14:13
    You might look for a particular
    piece of data.
  • 14:13 - 14:15
    You might like iterate over all the data.
  • 14:15 - 14:19
    Certainly you could think about how you
    could draw something based on the data.
  • 14:19 - 14:23
    Like maybe you would draw circles tied to
    the number of members of each family of birds.
  • 14:23 - 14:28
    Animate that with flying birds on
    the screen with their name.
  • 14:28 - 14:32
    Think of all sort of creative things
    you could do besides just...
  • 14:32 - 14:36
    Instead of just literally displaying the data
    on the web page.
  • 14:36 - 14:41
    So this I think... Kind of...
    Keep going with this...
  • 14:41 - 14:44
    You're going to have a different data set,
    it's going to look different.
  • 14:44 - 14:46
    You're going to have your own
    sense of how to navigate it.
  • 14:46 - 14:49
    Hopefully this will give you a sense.
    Practice this.
  • 14:49 - 14:52
    Get a JSON file. Make it yourself.
    Find a reference on line.
  • 14:52 - 14:55
    Load it... Put it in your P5 sketch.
    Load it into a variable.
  • 14:55 - 14:58
    See if you can pull out a single piece of data.
  • 14:58 - 15:00
    See if you can all out all the data.
    Can you get that to work.
  • 15:00 - 15:02
    I encourage you to use this 'corpora'.
  • 15:02 - 15:07
    A repository that has all sorts of goofy and like
    random sets of data in it.
  • 15:07 - 15:09
    And hopefully that will get you started.
  • 15:09 - 15:12
    But what I want to do in the next video is
  • 15:12 - 15:15
    look at how you might
    pull this data from a URL.
  • 15:15 - 15:21
    So what if you have a
    www.something.com /something/json
  • 15:21 - 15:25
    That you could pole... Rather than just pole
    this local file that's in your project.
  • 15:25 - 15:28
    The reason why that could be useful is
    somebody else might prepare
  • 15:28 - 15:32
    a JSON file for you that's dynamic. That's
    changing and you could query it.
  • 15:32 - 15:36
    Which is like one step closer to this
    idea of an API.
  • 15:36 - 15:41
    Of using someone else's system that you
    querying to get a certain amount
  • 15:41 - 15:45
    of JSON data back.
  • 15:45 - 15:48
    So... Alot of this probably
    didn't make any sense...
  • 15:48 - 15:51
    Or you might have turned this off
    long ago...
  • 15:51 - 15:53
    But if you are still watching...
  • 15:53 - 16:08
    Say something to me that you are watching.
    Never mind...
Title:
8.3: What is JSON? Part II - p5.js Tutorial
Description:

more » « less
Video Language:
English
Duration:
16:10

English subtitles

Revisions