Hey, everyone, in this video, we're going to talk a very, very important subject, which is, Jason,
if you work with Web design and web development, even if you're just starting out, I'm sure you already
heard about Jason.
Jason is a lightweight format for storing and transporting data between applications.
And the great thing about Jason is that all the most popular programming languages offer support for
reading and writing data.
And Jason, so most of the times that you work with external sources of data, so let's say you get
data from an external API.
Most likely that data is going to come to you as Jason and no matter which programming language you're
working with.
Maybe you're working with C Sharp or B or JavaScript.
All of them, they have support for Jason, so you will be able to work with that data for JavaScript
programmers, Jason couldn't be more convenient because it consists basically of JavaScript objects.
In fact, Jason stands for JavaScript object notation.
So down here I have an example of a JavaScript object.
Which, as we know, is just a set of key value pairs, so what Jason is, is a string representation
of an object.
So if I had this as Jason, I would have a string that would just represent this object, but the problem
about strings is that we already know that if we store something like this as a string, then it's going
to be very hard to work with the data, like when we want to get the name of the employee or the birthdate
is going to be pretty much impossible if this is stored as string.
But we shouldn't worry about this because every programming language, we'll have methods for importing
and exporting data.
And Jason.
So if we receive this thing as a string, we are going to convert it back into a JavaScript object so
we can work with it.
And this is what we're going to learn how to do right now.
So let me just copy this variable.
Let's take it to Visual Studio code.
Right.
So now we just created our object.
Now, let's say we want to convert this to Jason so we can send to an external application.
So I'm just going to create another variable called.
Employee Jason, just so we can see it better in the console and right here, I'm going to use a method
that comes with JavaScript.
So you call the JSON library.
And then we can use the string ifI.
Method to convert this object into Jason, so the way we use it is like this we call the method and
we just passed the object that we want to convert in this case.
We just want to convert this object.
Right.
So this is going to transform this object into Jason.
And now that we have this, let's send it to the console and let's see what is going to be printed in
the console.
I'm just going to save this.
And going back to the page now, as you can see, it seems that we still have that same object, but
now if we do a type of and try to get the type of that variable, as you can see, this data is now
stored as a string.
So I can't do like employee Jason and try to get the name if I do this.
So employee Jason, that name.
As you can see, this is returning undefined because this is not an object anymore.
Now this is a string.
So this is what, Jason, this is just a string representation of an object.
So what exactly is the advantage of doing this?
This is because every programming language that we know, they have a data type called string.
So by storing data in a string, then this is a very lightweight formant and very easy to send to different
applications that use different programming languages.
If we send this to an application that's using B, it's no problem.
We just need to use a method that is going to convert a string, which in this case is a Jason into
a B object and the same thing for any other programming language.
So this is why Jason is so powerful.
Now let's see how we can do the opposite.
So let's say we just received this, Jason, from an external source and now we need to convert it back
to an object in order to work with it.
So going to Visual Studio Code, we know that we have a Jason, which is basically a string, a string
representation of an object, and this is stored inside this variable.
So let's say we just got it from an external source.
So now how do we convert it back into a JavaScript object for this?
I'm going to create another one again just to make things easier when we check this variable from the
console.
So I'm just going to create another one called employee.
OBD.
Which I'm going to use to cover this string back into an object.
So we are going to use the Jason Library again, and now the name of the method is pass, so by using
the pass.
JavaScript is going to convert this string into an object.
So inside here, I'm just.
Going to pass the Jason that I want to convert.
Now, let's do consolidate log for this variable.
So as you can see now, we have this converted back into an object, if we do type of.
Now we can see that it's not a string anymore, now it's an object, and since this is an object, we
can get its properties.
So we could just do like employee object that name and we can easily work with this objects, properties.
Please note that not only objects can be converted into JSON, but we can do the same thing with arrays.
So down here I've just included an example, which is just an array, which is a list of values here.
We don't have key value pairs, we just have an array.
And the same way we can use the string Jeffie to convert it into a Jason.
So let's do it quickly in the console just so we can see.
So now we just have an array.
And if I do this so I'm going to string defying this array or convert into Jason.
So if I do this, as you can see, it's just converted into a string, so it works the same way as an
object, actually, let's remember that if we do type of.
Group A, let's remember that JavaScript treats arrays as objects, so maybe when you receive data from
an external source, maybe it's going to be an array or an object, or maybe it's just going to be multidimensional
arrays mixed with object, which is what is most common out there.
So this is really, really cool because now we can start sending requests to API and start working with
external data.
This is what we are going to do in the next section when we learn how to work with Ajax.
I'm just going to show you a very quick example here of what we are going to work with.
So I'm going to visit a website, which is the trivia API.
Here it is.
Open trivia DBI and what this is, is just a database of trivia questions and answers, so let's just
get a quick example here.
Let's get one question and then you have multiple categories like entertainment, science, sports.
Let's.
Let's select history and let's just leave all the rest as default and let's generate an API euro.
So as you can see, this is the the URL to call this API.
And what happens is if we use this with JavaScript and Ajax, this is what we are going to have in return.
So I'm just going to call it now.
And as you can see, what this is going to return to us is a JSON.
So this is a trivia question with the answers so we can create a very cool.
Game using JavaScript, and this is what we are going to do in the next section when we have this,
we just need to convert into an object and then we can work with all the properties.
We have the category type difficulty, we have the actual question, and every time we call this you
are, we are going to have a different question, so I'm going to load it again.
So now, as you can see, we have a different question so we can create a very cool game that's going
to get random questions from this database.
So this is what we're going to do in the next section.
So that was all I wanted to talk about, Jason, for the moment.
I'll see you in the next video.