Hi everybody my name is Joanne and in this video we will be learning about
JSON so what is JSON? JSON stands for JavaScript Object Notation
and it is a data of representation format it's used for transferring data
between server and clients and also for configuration files JSON serves the same
purpose as XML but it has a lot of advantages over XML so why use JSON JSON
is lightweight so it's easier to get and load data very quickly it's also
scalable JSON actually is language independent which means it can work with
most of our modern programming languages actually my first experience with JSON
was not within a JavaScript program it was actually using C# API the nice
thing about it being scalable is that if you have server code and it's always
writing out JSON it doesn't matter if you change your server from being a
C# web API to some type of Java API it
doesn't matter as long as you're still giving back the same JSON message
finally it has a standard structure and the nice thing about it having a
standard structure is that it's really easy to read and write the code so let's
look at the differences between JSON and XML one side you can see that I have
JSON for cats this is an array of the object cat and on the other side is our
XML code you can see that there's significantly less tags in the JSON and
you can really just clearly see that JSON is more lightweight compared to XML
and on top of that JSON has the concept of arrays. Which is not available in XML.
Actually here I'm illustrating a couple different types in JSON I have a number
type a string an object and an array but for our brackets so JSON has several
different types we have strings numbers that can be represented in many
different ways boolean's which is true or false are arrays which are surrounded
with we're bracket and a comma space are
objects which are denoted by a curly bracket and on top of it
no so now that you've learned high-level about JSON let's write a JSON file so I
have an empty file here called JSON example dot JSON so JSON file has the
extension JSON now a JSON file can just hold a single type in it you could say
true this is actually valid JSON or if you did a string
you could just say string same down to just saying null but that's not really
useful it's only one item and if you want it to grow it doesn't work so most
of the time a JSON file is either an array or an object so it usually either
starts off with a bracket indicating an array or our curly brackets for an
object let's write that cat that we've done so in a JSON file I would start it
off by saying curly bracket and within that this would hold my object so if I
wanted to make an array of cats I would say I have an array named cats so this
is the name is cats and then to denote its type I start with a bracket so now
this cats is an array in this case cats is an array with nothing in it so I want
to add an object and I'm gonna start off with making my first cats so I'm gonna
enter down a little and we'll start off with our cat so I can make first name
and save its name and I have two cats and one cat's name is algebra and she is
10 years old and the type of cat she is it's a calico now I want to add another
object to this ring because I have two cats we put a comma and then we didn't
know that we have another object coming so we don't end with a comma that's not
valid JSON so when you're done don't have a lingering comma so here I have a
cat named Chell from Portal she is
seven years old and she's like a gray tabby calico so it's a type gray tabby
although that's not truly her type and she'll be very upset about that if she
finds out so this is the list of cats I have this an array of cat types you can
add more to it you don't have to just have one array in here so I have dogs so
I'm putting a comma and now I'm saying dogs I actually only have one dog but
maybe I can convince my husband one day to let me have another dog so we can
make an array again with our object and in this case I have an array of one dog
name is Leela Futurama age is five and type is mutt now we have created a JSON
file that holds two arrays one is an array of cats the one is an array of
dogs we have our strings and we're also holding a number and then we have our
type as well which is another string if we wanted to we can maybe have a
boolean let's say if they're spay or neutered right so is
it means a true and that's true also for all these others maybe this is a file
that holds a configuration of all animals that people have and maybe
because other people have Birds we want to capture Birds but I don't have any
birds so I can say birds here and instead of giving back an empty array I
can also give back a null and that means there is nothing there for birds so now
you've learned how to work with JSON