[MUSIC]
>> My name is Nitya Narasimhan.
I'm a Cloud Advocate at Microsoft.
Today, I want to talk to you about JavaScript object notation.
There are just four things you need to know so let's get started.
First, I want to set some context for why JSON is useful.
It turns out that JavaScript is
actually used extensively in web development today,
where you have clients or applications.
You might also be use familiar with its use in Node,
where you have many cases where you need to
read and write data from the file system.
Both communication protocols and
file transfer protocols tend to rely
on text-based formats for data transfer.
This is where JSON comes in.
JSON is a lightweight data exchange format
that is language-independent and user readable.
In fact, in the example on the right side,
you can look at what a JavaScript object literal,
in this case, a book with three properties;
title, author and is available,
looks like when it is converted into
an equivalent JSON string shown below in black.
Pretty readable.
JSON is a language-independent format because
any modern programming language that supports
two kinds of data structures can use JSON.
The first is name-value pairs.
Any programming language that sports
the ability to have a collection of name value pairs.
In JavaScript, this is probably very familiar to you.
Doesn't that look like an object with properties?
The second data structure is an ordered list of values.
While name-value pairs let you serialize a single object,
if you want to pack multiple objects into a single string,
you need an array.
These two data structures are supported in
most modern programming languages.
Here's what that looks like when you use it with JavaScript data.
So the JSON format
effectively is name-value pairs for a single object.
It encloses name-value pairs separated by
commas within curly braces as shown in the highlighted yellow.
When you have multi-item or complex arrays serialized,
it encloses all those multiple objects, again,
separated by commas inside square blue brackets.
Both of these are very usable user-readable strings.
But how do you convert between JavaScript objects and JSON?
Well, you just need to know two methods.
First, objects to JSON, you stringify them.
That's easy to remember because
you're converting it into string format.
What I want you to look at in these two examples,
the first one serializes
the JavaScript object into a single text string.
The second serializes a collection of
JavaScript objects into a text string
which represents an ordered list.
What I want you to pay attention to is both of these
used the exact same method, JSON.stringify,
and pass in a reference to the data that you want serialized,
whether it's an object on an array,
you don't need to have any kind of difference between them.
Once you stringify it,
how do you then de-serialize
or restore the original objects from that?
Well, just remember JSON.parse.
Again, if you have a serialized string,
you don't even need to know whether that
came from an object or a collection.
Simply pass it to JSON.parse and alert on your reference to
a JavaScript object or possibly an array using typeof.
With that returned result will
let you know the difference between the two.
But what I want you to look at here is that the JSON,
that the JSON string once de-serialized,
gives an object that's effectively got the properties restored.
What if you needed to have the object
define the functions as well?
JSON is not going to do that for you.
The best way to do that is to have
a constructor that's capable of picking the properties and
its data that serialized and then restoring it to
a complete object with the functions defined
as well using a custom constructor.
With that, that's literally all you needed to know about JSON.
So hopefully, you've got to understand what JSON is,
what the JSON format looks like,
how to convert JavaScript objects to JSON,
and how to restore a JavaScript object from a JSON string.
Hope that was fun. See you at the next module.