Intro to JSON
Transcript
hi everyone so this is just a quick
review or preview of how to create your
own JSON document which is the
assignment for this week and what you're
going to be doing is creating a JSON
document that describes items or things
in your life or things about your life
and the purpose of this is to get us
familiar with the structure of JSON
documents and also understand how they
can be made as descriptive and
meaningful as possible okay so I am
going to do a demo here with some books
but you should shoot something other
than books when you are creating your
own JSON document so I've increased the
font here I'm actually working on a PC
so you'll see I'm using notepad instead
of TextEdit just remember when you're
using TextEdit that you want to make
sure to convert TextEdit to make plain
text and you'll also want to go into the
preferences and make sure that you
uncheck the box that says use smart
quotes and use smart dashes you won't
really use the dashes but you might as
well get rid of them
keep in mind you'll probably need to
close whatever document you're working
on to start a new document to make sure
that that change takes effect but you
want to be careful that the quotation
marks that show up for you are not curly
and not angled right they should be like
just straight so starting on my jason
document it's always gonna start the
same way it's gonna start with curly
brace and as we mentioned in class
everything in a JSON document is always
going to be wrapped in quotes
unless it's an object or a list right so
basically anytime I have kind of a
standalone value it's gonna be in quotes
whether it's a number or a word or
several words it's always gonna be
wrapped in quotes and so in this case
the the my main key right remember the
thing that appears on the left is the
key my main key is gonna be books right
because that's what this document is
about it is about books and then within
that right I'm creating another object
okay so within the book object I'm
creating another object and I'm gonna
have a couple of kind of top what I'm
thinking of sort of top-level parameters
here so the first one is gonna be I'm
gonna say who owns these books right so
that's my uni right there
now the question of sort
how to format this generally speaking we
want to do an indent for every level
down that we go right so every time I
have an object or a list that appears as
part that's inside another one if you
remember the circles that we drew in
class I want to do an indent and that's
to give a visual hierarchy to my
document right the computer is gonna
understand it either way this is for the
humans right for us so we can better
understand what's going on here so
you'll notice that I have one key which
is owner and then the value is my uni in
this case I'm actually gonna even do
this because I like formatting comes out
better and then I'm also gonna say okay
the category of books that I'm gonna put
in this is poetry books ok so again
that's another key value pair and then
I'm gonna have a third key value pair
and that's going to be volumes okay so
as in the volumes of poetry now volumes
is going to be a list right and so again
always indicator of a list is square
brackets now one trick for this which I
probably should have started out doing
is remember that I need to match all my
curly braces what I'm starting a list I
go ahead and do the open and closing
brackets right away right and then I
just add some space between them and
what's handy about that it's kind of
doing that automatic matching right
putting in both both braces when you
start to create something is that you
don't have to worry as much about making
sure that you close them all at the end
right because you've already it's
already there somewhere right you just
doesn't make sure that the syntax before
that is correct so again I've now
created a list I'm making a list of
objects and so we didn't that list of
objects I'm going to have books and so
each book has lots of potential
attributes right I'm gonna only category
gonna catalog a few of them here so I'm
gonna say the title is still rise okay
another feature that so again I can kind
of do this now this isn't necessarily
exactly how a program like Jason lint is
gonna format it for us that's okay what
you want to do I mean we're gonna put it
into that and that's going to correctly
format it for
but even now I like this because it
makes it clear kind of where things line
up in fact I might even actually put
this having it on a full tab is a little
much in fact I think this is sort of how
they prefer to do it right right so I
can again I'm still giving it kind of a
visual hierarchy here that makes it easy
to see sort of what is a subset of what
okay so I'm working within this object
right and again I can I could create the
closing tag to that object right away
and so I kind of have it handy but I'm
gonna say the title the author
[Music]
publication year now I want to point out
something that I'm doing here with a
publication year so first of all notice
that notice at 1978 although it's a
number still goes in double quotes
secondly publication year I've put an
underscore between publication and year
okay and the reason for this is that for
any key we generally don't want special
characters in our keys right anything
that appears on the left
we don't want special characters in that
okay spaces are considered special
characters any kind of punctuation I
have a say wouldn't want to comma or a
period or slash or anything like that
really I just want to stick with numbers
letters and and basically underscores of
even dashes aren't a good idea in fact I
think dash is actually pretty good um so
I want to stick with these characters
now the reason why up a publication here
and there is just because it's a way for
me what I'm trying to do is be
descriptive right I would ideally like
someone to be able to look at this
document and without additional
information know what the data is that
it contains right be able to interpret
this information without without any
additional metadata right without any
additional information about the file
and so to that end I want my keys to be
as descriptive as possible right but you
know as they start to get long
especially if you have multiple words
and you get kind of hard to read them
right so the easiest way to deal with
that
just to put an underscore can also use
something called camelcase which is
where the first word in a multi word key
is or to word key is it can actually be
as many words as you want but in the
first word has a lowercase that every
subsequent word has an uppercase
beginning it and that actually makes it
much more readable so it's really up to
you which you prefer but that's the
thing you want out for note watch out
for no special characters in your keys
so now if I wanted to do this I need to
and want to have an additional item here
right one thing that I can do is I can
actually just copy my first one right
and then go ahead and change the values
right and the reason why the reason why
I might do this is that you know once
I've worked this out right there's no
point in my typing out all of the
individual key keys again because this
is a list
I want the all of the keys within each
object ought to be the same right
because they're the same type of object
right they're the same kind of the same
sort of category of thing right there
poetry books so I want to make sure that
the the keys are all the same because
that's what will let me eventually kind
of loop through this list and look
through each item ago okay what are the
titles of all of the books in this list
what are the authors of all the books in
this list so I'm gonna do this you
notice again actually that's not my
Anjali but we'll just leave that moment
whoops
lost one there okay so now I can see
pretty clearly that I've got open pasted
this one's opened and closed it's got a
comma this one is open and closed no
comma again because it's the last item
okay so this is in good shape now I've
got one up here and I'm gonna just go
ahead and put that in we got one up here
and I think that I am in pretty good
shape here so let's go ahead I'm gonna
copy this then I'm gonna put this in
into this
tool called JSON lint okay and so what
this does is this is gonna this is gonna
clean up any formatting issues that we
have and it's also gonna tell me if it's
valid JSON right so I just click that
and luckily it is valid right someone
asked in class is that's in class what
happens if I forget a comma right is it
just gonna put it on one line it is not
it is going to say look something wrong
and you can see that it has gone ahead
and pretty helpfully highlighted where
something has gone wrong here right it's
like there's something wrong on this
line and so again this is something you
all will get more experienced at as you
do this more but what but what you can
see from this is that so you'll get more
experience at recognizing like oh right
I'm missing a comma there right and then
you go on and you validate your JSON
okay so each of you will produce a
similar file you will not use books but
you're gonna have at least two what I
call top-level key value pairs the keys
that you use must be different okay so
you can't have owner category in volumes
or books right you need to have I don't
know
albums genre some pattern but these
should all be different for you so
that's it if you have any questions
please post them to Piazza and keep an
eye out for more videos coming later
this week it will be a preview of next
week's lesson along with a handful of
readings and yep I look forward to
seeing you all then
review or preview of how to create your
own JSON document which is the
assignment for this week and what you're
going to be doing is creating a JSON
document that describes items or things
in your life or things about your life
and the purpose of this is to get us
familiar with the structure of JSON
documents and also understand how they
can be made as descriptive and
meaningful as possible okay so I am
going to do a demo here with some books
but you should shoot something other
than books when you are creating your
own JSON document so I've increased the
font here I'm actually working on a PC
so you'll see I'm using notepad instead
of TextEdit just remember when you're
using TextEdit that you want to make
sure to convert TextEdit to make plain
text and you'll also want to go into the
preferences and make sure that you
uncheck the box that says use smart
quotes and use smart dashes you won't
really use the dashes but you might as
well get rid of them
keep in mind you'll probably need to
close whatever document you're working
on to start a new document to make sure
that that change takes effect but you
want to be careful that the quotation
marks that show up for you are not curly
and not angled right they should be like
just straight so starting on my jason
document it's always gonna start the
same way it's gonna start with curly
brace and as we mentioned in class
everything in a JSON document is always
going to be wrapped in quotes
unless it's an object or a list right so
basically anytime I have kind of a
standalone value it's gonna be in quotes
whether it's a number or a word or
several words it's always gonna be
wrapped in quotes and so in this case
the the my main key right remember the
thing that appears on the left is the
key my main key is gonna be books right
because that's what this document is
about it is about books and then within
that right I'm creating another object
okay so within the book object I'm
creating another object and I'm gonna
have a couple of kind of top what I'm
thinking of sort of top-level parameters
here so the first one is gonna be I'm
gonna say who owns these books right so
that's my uni right there
now the question of sort
how to format this generally speaking we
want to do an indent for every level
down that we go right so every time I
have an object or a list that appears as
part that's inside another one if you
remember the circles that we drew in
class I want to do an indent and that's
to give a visual hierarchy to my
document right the computer is gonna
understand it either way this is for the
humans right for us so we can better
understand what's going on here so
you'll notice that I have one key which
is owner and then the value is my uni in
this case I'm actually gonna even do
this because I like formatting comes out
better and then I'm also gonna say okay
the category of books that I'm gonna put
in this is poetry books ok so again
that's another key value pair and then
I'm gonna have a third key value pair
and that's going to be volumes okay so
as in the volumes of poetry now volumes
is going to be a list right and so again
always indicator of a list is square
brackets now one trick for this which I
probably should have started out doing
is remember that I need to match all my
curly braces what I'm starting a list I
go ahead and do the open and closing
brackets right away right and then I
just add some space between them and
what's handy about that it's kind of
doing that automatic matching right
putting in both both braces when you
start to create something is that you
don't have to worry as much about making
sure that you close them all at the end
right because you've already it's
already there somewhere right you just
doesn't make sure that the syntax before
that is correct so again I've now
created a list I'm making a list of
objects and so we didn't that list of
objects I'm going to have books and so
each book has lots of potential
attributes right I'm gonna only category
gonna catalog a few of them here so I'm
gonna say the title is still rise okay
another feature that so again I can kind
of do this now this isn't necessarily
exactly how a program like Jason lint is
gonna format it for us that's okay what
you want to do I mean we're gonna put it
into that and that's going to correctly
format it for
but even now I like this because it
makes it clear kind of where things line
up in fact I might even actually put
this having it on a full tab is a little
much in fact I think this is sort of how
they prefer to do it right right so I
can again I'm still giving it kind of a
visual hierarchy here that makes it easy
to see sort of what is a subset of what
okay so I'm working within this object
right and again I can I could create the
closing tag to that object right away
and so I kind of have it handy but I'm
gonna say the title the author
[Music]
publication year now I want to point out
something that I'm doing here with a
publication year so first of all notice
that notice at 1978 although it's a
number still goes in double quotes
secondly publication year I've put an
underscore between publication and year
okay and the reason for this is that for
any key we generally don't want special
characters in our keys right anything
that appears on the left
we don't want special characters in that
okay spaces are considered special
characters any kind of punctuation I
have a say wouldn't want to comma or a
period or slash or anything like that
really I just want to stick with numbers
letters and and basically underscores of
even dashes aren't a good idea in fact I
think dash is actually pretty good um so
I want to stick with these characters
now the reason why up a publication here
and there is just because it's a way for
me what I'm trying to do is be
descriptive right I would ideally like
someone to be able to look at this
document and without additional
information know what the data is that
it contains right be able to interpret
this information without without any
additional metadata right without any
additional information about the file
and so to that end I want my keys to be
as descriptive as possible right but you
know as they start to get long
especially if you have multiple words
and you get kind of hard to read them
right so the easiest way to deal with
that
just to put an underscore can also use
something called camelcase which is
where the first word in a multi word key
is or to word key is it can actually be
as many words as you want but in the
first word has a lowercase that every
subsequent word has an uppercase
beginning it and that actually makes it
much more readable so it's really up to
you which you prefer but that's the
thing you want out for note watch out
for no special characters in your keys
so now if I wanted to do this I need to
and want to have an additional item here
right one thing that I can do is I can
actually just copy my first one right
and then go ahead and change the values
right and the reason why the reason why
I might do this is that you know once
I've worked this out right there's no
point in my typing out all of the
individual key keys again because this
is a list
I want the all of the keys within each
object ought to be the same right
because they're the same type of object
right they're the same kind of the same
sort of category of thing right there
poetry books so I want to make sure that
the the keys are all the same because
that's what will let me eventually kind
of loop through this list and look
through each item ago okay what are the
titles of all of the books in this list
what are the authors of all the books in
this list so I'm gonna do this you
notice again actually that's not my
Anjali but we'll just leave that moment
whoops
lost one there okay so now I can see
pretty clearly that I've got open pasted
this one's opened and closed it's got a
comma this one is open and closed no
comma again because it's the last item
okay so this is in good shape now I've
got one up here and I'm gonna just go
ahead and put that in we got one up here
and I think that I am in pretty good
shape here so let's go ahead I'm gonna
copy this then I'm gonna put this in
into this
tool called JSON lint okay and so what
this does is this is gonna this is gonna
clean up any formatting issues that we
have and it's also gonna tell me if it's
valid JSON right so I just click that
and luckily it is valid right someone
asked in class is that's in class what
happens if I forget a comma right is it
just gonna put it on one line it is not
it is going to say look something wrong
and you can see that it has gone ahead
and pretty helpfully highlighted where
something has gone wrong here right it's
like there's something wrong on this
line and so again this is something you
all will get more experienced at as you
do this more but what but what you can
see from this is that so you'll get more
experience at recognizing like oh right
I'm missing a comma there right and then
you go on and you validate your JSON
okay so each of you will produce a
similar file you will not use books but
you're gonna have at least two what I
call top-level key value pairs the keys
that you use must be different okay so
you can't have owner category in volumes
or books right you need to have I don't
know
albums genre some pattern but these
should all be different for you so
that's it if you have any questions
please post them to Piazza and keep an
eye out for more videos coming later
this week it will be a preview of next
week's lesson along with a handful of
readings and yep I look forward to
seeing you all then