Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
1
00:00:00,000 --> 00:00:07,500
Hello everyone, my name is Kyle, and this is Web Dev Simplified, where we make the web easy to understand and accessible for everyone.
2
00:00:07,500 --> 00:00:15,500
In this video, I'm going to be going over JSON, which is one of the most important concepts that you can learn as a programmer or as a web developer.
3
00:00:15,500 --> 00:00:21,500
I'm going to be going over what JSON is, why you should know it, and all of the syntax involved with JSON,
4
00:00:21,500 --> 00:00:27,000
and at the end of the video, I'm going to go through examples of JSON, so make sure you stick around till the end.
5
00:00:30,000 --> 00:00:39,000
JSON, also known as JavaScript Object Notation, is simply a data representation format very similar to XML or YAML.
6
00:00:39,000 --> 00:00:44,500
It's used widely across the internet for almost every single API that you will access,
7
00:00:44,500 --> 00:00:52,500
as well as for config files and things such as games, in text editors like VS Code, and many, many other places throughout programming.
8
00:00:52,500 --> 00:00:58,000
It's used because it's extremely lightweight to send back and forth due to its small file size,
9
00:00:58,000 --> 00:01:05,500
it's easy to read compared to something like XML, since it's much cleaner and there's not as many opening and closing tags to worry about,
10
00:01:05,500 --> 00:01:12,500
and it also integrates very nicely with JavaScript, since JSON is just a superset of JavaScript,
11
00:01:12,500 --> 00:01:16,500
which means anything you write in JSON is valid JavaScript,
12
00:01:16,500 --> 00:01:23,000
so it integrates nicely with JavaScript, which is used all throughout the web for frontend or backend of web applications.
13
00:01:23,000 --> 00:01:33,500
Also, almost every single major language has some form of library or built-in functionality to parse JSON strings into objects or classes in that language,
14
00:01:33,500 --> 00:01:38,500
which makes working with JSON data extremely easy inside of a programming language.
15
00:01:38,500 --> 00:01:42,000
Throughout your programming career, you're going to use JSON all the time,
16
00:01:42,000 --> 00:01:50,000
whether it's creating an API, consuming an API, or creating config files for you to use or for other people to use for your application.
17
00:01:50,000 --> 00:01:53,000
Now that we understand what JSON is and why it's important,
18
00:01:53,000 --> 00:01:59,500
let's dive into some of the syntax involved with JSON by starting by talking about the types that JSON can represent.
19
00:01:59,500 --> 00:02:05,500
As we know, JSON is a data representation format, so we need to be able to represent certain data types within it,
20
00:02:05,500 --> 00:02:11,000
and JSON natively supports strings, numbers, and these numbers can be in any format,
21
00:02:11,000 --> 00:02:17,000
whether they're decimal numbers, whole numbers, negative numbers, even scientific notation numbers.
22
00:02:17,500 --> 00:02:21,000
It supports booleans, which can be either true or false.
23
00:02:21,000 --> 00:02:24,000
It supports null, which essentially stands for nothing,
24
00:02:24,000 --> 00:02:28,000
arrays, which can be a list of any of the types that we've talked about,
25
00:02:28,000 --> 00:02:31,000
plus the next type, which is the final type of object.
26
00:02:31,000 --> 00:02:35,000
An object is the most complex but most used type within JSON,
27
00:02:35,000 --> 00:02:39,000
and it allows you to represent values that are key-value pairs,
28
00:02:39,000 --> 00:02:45,000
so you give it a key and then a value, and that value can be anything of the other types we've talked about,
29
00:02:45,000 --> 00:02:50,000
string, number, boolean, array, null, any of those different types can be the value for a key.
30
00:02:50,000 --> 00:02:54,000
So let's dive into an example of how to use JSON inside of a file.
31
00:02:54,000 --> 00:03:00,000
The first thing that you need to do is to create a file with the .json extension at the end of it,
32
00:03:00,000 --> 00:03:05,000
so that's .json at the end of your file, and that'll create a JSON file.
33
00:03:05,000 --> 00:03:11,500
Inside of this JSON file, what you do is you take one of the types that we talked about and you put that inside of the file.
34
00:03:11,500 --> 00:03:15,500
So for example, you could put a string, you could put a number, a boolean,
35
00:03:15,500 --> 00:03:19,500
whatever type you want inside of that file, and that's valid JSON.
36
00:03:19,500 --> 00:03:23,000
But if all you have is a single string or a single number,
37
00:03:23,000 --> 00:03:27,000
it's really not very useful to have a whole file for that single input.
38
00:03:27,000 --> 00:03:29,500
So what most of the time when you're working with JSON,
39
00:03:29,500 --> 00:03:34,000
you'll have either an array or an object as your top level of your file,
40
00:03:34,000 --> 00:03:37,500
and then inside of that array or object, you'll have other values,
41
00:03:37,500 --> 00:03:40,500
so it may even have other objects, have other arrays,
42
00:03:40,500 --> 00:03:44,500
or even just individual values such as strings or numbers inside of that.
43
00:03:44,500 --> 00:03:50,500
So let's take an example of a user object that we would want to put at the top level of our JSON file,
44
00:03:50,500 --> 00:03:53,000
which we're going to call user.json.
45
00:03:53,000 --> 00:03:57,000
To create an object, we need to use opening and closing curly braces,
46
00:03:57,000 --> 00:04:02,000
and then inside of that, we'll put all of the key value pairs that make up our object.
47
00:04:02,000 --> 00:04:04,000
In every single one of these key value pairs,
48
00:04:04,000 --> 00:04:08,500
the key must be surrounded by double quotes followed by a colon,
49
00:04:08,500 --> 00:04:11,000
and then the value for that key.
50
00:04:11,000 --> 00:04:13,000
And then if we have multiple key value pairs,
51
00:04:13,000 --> 00:04:17,000
we need commas separating every single one of our key value pairs,
52
00:04:17,000 --> 00:04:21,500
similarly to how we would create an array in a normal programming language.
53
00:04:21,500 --> 00:04:28,000
So for example, if our user has a name, we would surround that name key in double quotes,
54
00:04:28,000 --> 00:04:29,500
put a semicolon after it,
55
00:04:29,500 --> 00:04:33,500
and then we would put the value of our name inside of double quotes as well,
56
00:04:33,500 --> 00:04:36,000
because it's a string and it must be in double quotes,
57
00:04:36,000 --> 00:04:40,500
and then at the end of that, we'll put a comma because we have other key value pairs for our user.
58
00:04:40,500 --> 00:04:44,500
For example, if we wanted to use a favorite number as another property,
59
00:04:44,500 --> 00:04:47,000
we would put favorite number in double quotes,
60
00:04:47,000 --> 00:04:51,000
followed by a semicolon, and then put that user's favorite number.
61
00:04:51,000 --> 00:04:55,000
Then if we wanted to use a Boolean, we use a comma,
62
00:04:55,000 --> 00:04:59,000
and then another property, we would use isProgrammer as our key,
63
00:04:59,500 --> 00:05:03,500
put a colon, and then we would put either true or false with no quotes around it
64
00:05:03,500 --> 00:05:06,500
to signify that this is a Boolean and not a string,
65
00:05:06,500 --> 00:05:10,500
so we would put true or false depending on if that user was a programmer or not.
66
00:05:10,500 --> 00:05:14,500
We could then go down and use hobbies as our next key value pair,
67
00:05:14,500 --> 00:05:17,000
so put hobbies in double quotes, semicolon,
68
00:05:17,000 --> 00:05:21,000
and then to create an array, we use opening and closing square brackets,
69
00:05:21,000 --> 00:05:25,500
and inside of the array, we put whatever type of value we want,
70
00:05:25,500 --> 00:05:28,500
and in this case, we're just going to use strings for the different hobbies.
71
00:05:28,500 --> 00:05:31,000
So we'll put those strings inside of double quotes,
72
00:05:31,000 --> 00:05:33,000
and we'll put commas between each one of them
73
00:05:33,000 --> 00:05:39,000
because we put commas between all the values in an array when we're writing out JSON.
74
00:05:39,000 --> 00:05:43,000
Next, we could have another property that will be an array of friends,
75
00:05:43,000 --> 00:05:45,500
but instead of putting strings inside of this array,
76
00:05:45,500 --> 00:05:49,000
we're going to put more user objects inside of that array,
77
00:05:49,000 --> 00:05:52,000
so we can put different user objects inside of that array,
78
00:05:52,000 --> 00:05:55,000
and now we have nesting of arrays with objects,
79
00:05:55,000 --> 00:05:58,000
and that's how you really get into the power of JSON,
80
00:05:58,000 --> 00:06:00,500
where you can start to deeply nest different properties
81
00:06:00,500 --> 00:06:02,500
and really show a hierarchy of data
82
00:06:02,500 --> 00:06:07,500
as opposed to just a flat format of data like most data format files give you.
83
00:06:07,500 --> 00:06:09,500
So for example, for this friends array,
84
00:06:09,500 --> 00:06:12,500
we could have a friends array and it could have the same properties of name,
85
00:06:12,500 --> 00:06:16,500
favorite number, is programmer, hobbies, and even friends inside of that,
86
00:06:16,500 --> 00:06:20,500
and you can start to get a very deeply nested array if you really want it.
87
00:06:20,500 --> 00:06:23,500
Then we just have to make sure that we don't put a comma
88
00:06:23,500 --> 00:06:27,500
on the very last property value key pair that we have,
89
00:06:28,000 --> 00:06:32,000
close it with a curly brace, and that's our full JSON file created.
90
00:06:32,000 --> 00:06:34,000
Now that we understand what JSON is
91
00:06:34,000 --> 00:06:37,000
and the syntax involved with writing JSON,
92
00:06:37,000 --> 00:06:40,500
I'm going to jump into a live example of me writing a JSON file
93
00:06:40,500 --> 00:06:43,000
and then parsing that file in JavaScript.
94
00:06:43,000 --> 00:06:45,000
So I have Visual Studio Code open
95
00:06:45,000 --> 00:06:48,500
and I'm inside of a file called companies.json,
96
00:06:48,500 --> 00:06:51,500
where we're going to store an array of different companies,
97
00:06:51,500 --> 00:06:54,000
and each of these companies is going to have a name,
98
00:06:54,000 --> 00:06:58,500
number of employees, a CEO, and their rating out of five.
99
00:06:58,500 --> 00:07:02,500
So let's get started by using our syntax for creating an array,
100
00:07:02,500 --> 00:07:05,500
which is to use an opening and closing square bracket,
101
00:07:05,500 --> 00:07:08,500
and inside of this array we're going to store different objects.
102
00:07:08,500 --> 00:07:11,000
So our first object is going to be our first company,
103
00:07:11,000 --> 00:07:13,500
which as we mentioned has a name,
104
00:07:13,500 --> 00:07:17,500
and we're just going to give that company a name of big corporation,
105
00:07:17,500 --> 00:07:20,000
and then we put the comma at the end of the row.
106
00:07:20,000 --> 00:07:23,000
We want to give it number of employees as well.
107
00:07:25,000 --> 00:07:28,000
And then this number of employees for this big corporation,
108
00:07:28,000 --> 00:07:30,500
we're going to say that they have 10,000 employees.
109
00:07:30,500 --> 00:07:32,500
Then we want to give them a CEO,
110
00:07:32,500 --> 00:07:36,000
and their CEO's name is going to be Mary.
111
00:07:36,000 --> 00:07:43,000
And then lastly, their rating out of five stars is going to be a 3.6.
112
00:07:43,000 --> 00:07:45,500
And now we want to store a second company in this array,
113
00:07:45,500 --> 00:07:49,000
so we just put a comma at the end of our first company object,
114
00:07:49,000 --> 00:07:52,500
open up a new object, and then give it the same properties.
115
00:07:52,500 --> 00:07:54,000
So we'll say name,
116
00:07:54,000 --> 00:07:58,000
and we're just going to give this one the name of small startup.
117
00:07:58,000 --> 00:07:59,500
Go down to the next line.
118
00:07:59,500 --> 00:08:01,500
We got number of employees.
119
00:08:01,500 --> 00:08:04,000
In this case, they're just going to have three employees
120
00:08:04,000 --> 00:08:06,500
since they're a small company.
121
00:08:06,500 --> 00:08:08,000
CEO is next,
122
00:08:08,000 --> 00:08:11,000
and this company does not have a CEO because they're so small,
123
00:08:11,000 --> 00:08:13,000
so we're just going to put null here,
124
00:08:13,000 --> 00:08:16,000
which is okay to have different types inside of your JSON object
125
00:08:16,000 --> 00:08:19,000
because JSON doesn't care what types your different keys are.
126
00:08:19,000 --> 00:08:21,500
It just matters that you have keys and values.
127
00:08:21,500 --> 00:08:23,000
So we have null here.
128
00:08:23,000 --> 00:08:26,000
And then lastly, we're going to give them a rating.
129
00:08:26,000 --> 00:08:28,500
Make sure to put that in double quotes.
130
00:08:28,500 --> 00:08:32,000
And their rating here is just going to be 4.2 or 4.3.
131
00:08:32,000 --> 00:08:33,000
And there we go.
132
00:08:33,000 --> 00:08:35,500
That is our entire company's JSON object.
133
00:08:35,500 --> 00:08:37,500
And as you see, we have no errors.
134
00:08:37,500 --> 00:08:40,000
But if we, for example, didn't put the quote here,
135
00:08:40,000 --> 00:08:41,500
you see that we get an error,
136
00:08:41,500 --> 00:08:43,000
and that's because we're using VS code,
137
00:08:43,000 --> 00:08:46,000
and it tells us when we have errors inside of our JSON.
138
00:08:46,000 --> 00:08:49,000
So we know that our JSON is always going to be correctly formatted,
139
00:08:49,000 --> 00:08:51,000
just like this is here.
140
00:08:51,500 --> 00:08:54,500
Now it's looking up this index.html file here,
141
00:08:54,500 --> 00:08:56,500
which is just a super simple file
142
00:08:56,500 --> 00:08:58,500
that has an opening script tag
143
00:08:58,500 --> 00:09:01,500
so that we can put some JavaScript in here to run on our page.
144
00:09:01,500 --> 00:09:03,500
So let's create a variable.
145
00:09:03,500 --> 00:09:05,500
We're just going to call it companies.
146
00:09:05,500 --> 00:09:08,500
And we're going to copy everything from this companies.json,
147
00:09:08,500 --> 00:09:11,000
because as I mentioned at the beginning of the video,
148
00:09:11,000 --> 00:09:14,500
JSON, anything in JSON, is valid JavaScript.
149
00:09:14,500 --> 00:09:16,500
So we can take this JSON directly
150
00:09:16,500 --> 00:09:20,500
and just paste it into our companies variable,
151
00:09:20,500 --> 00:09:22,500
and then if we go down here
152
00:09:22,500 --> 00:09:26,000
and we log the companies variable,
153
00:09:26,000 --> 00:09:28,500
and we check that out inside of our browser here,
154
00:09:28,500 --> 00:09:31,000
as you can see, we have logged this companies variable,
155
00:09:31,000 --> 00:09:33,000
and we have both parts of our array.
156
00:09:33,000 --> 00:09:35,500
We have CEO name, number of employees rating,
157
00:09:35,500 --> 00:09:39,000
and all of this is the information that we have in this file here
158
00:09:39,000 --> 00:09:42,000
that we copied over into our JavaScript file.
159
00:09:42,000 --> 00:09:44,500
And same thing down here, we have our other company.
160
00:09:44,500 --> 00:09:48,000
But most of the time, when you're dealing with JSON,
161
00:09:48,000 --> 00:09:50,000
you're going to get it back as a string
162
00:09:50,000 --> 00:09:52,500
and not as an actual JavaScript object.
163
00:09:52,500 --> 00:09:55,500
So to emulate that, let's surround this in back ticks,
164
00:09:55,500 --> 00:09:57,000
so it's an actual string.
165
00:09:57,000 --> 00:09:59,000
And if you save that, you'll see that now,
166
00:09:59,000 --> 00:10:02,500
our console just has a string instead of an actual object,
167
00:10:02,500 --> 00:10:06,500
and we can't actually use this object inside of our JavaScript.
168
00:10:06,500 --> 00:10:11,000
So in order to convert this JSON string into a JavaScript object,
169
00:10:11,000 --> 00:10:13,500
we need to use what's called JSON.parse.
170
00:10:13,500 --> 00:10:18,000
So if we go down to our log, and we say JSON.parse,
171
00:10:18,000 --> 00:10:21,500
and we pass it in a string, it'll take that string
172
00:10:21,500 --> 00:10:24,000
and convert it into a JavaScript object.
173
00:10:24,000 --> 00:10:28,000
So now, as you can see, in here, we have our JavaScript object
174
00:10:28,000 --> 00:10:32,000
that we created from this string here using JSON.parse
175
00:10:32,000 --> 00:10:34,000
to get a JSON object right here.
176
00:10:34,000 --> 00:10:36,000
And we can use this inside of JavaScript.
177
00:10:36,000 --> 00:10:39,000
For example, if we wanted, we could get the first company
178
00:10:39,000 --> 00:10:42,000
inside of that array, and we could get their name.
179
00:10:42,000 --> 00:10:45,500
And now if we save that, you'll see it prints out the first company's name.
180
00:10:45,500 --> 00:10:48,500
You get the second company's name, prints it out, and so on.
181
00:10:48,500 --> 00:10:52,000
And you can do anything that you can do with a normal JavaScript object
182
00:10:52,000 --> 00:10:56,000
to this newly parsed JSON object that we created with JSON.parse.
183
00:10:56,000 --> 00:10:58,000
So I hope this video was useful for you.
184
00:10:58,000 --> 00:11:02,500
As you can see, the actual format for JSON is fairly straightforward.
185
00:11:02,500 --> 00:11:05,000
You just have to mostly remember to use double quotes
186
00:11:05,000 --> 00:11:08,000
around all of your different keys, because in JavaScript,
187
00:11:08,000 --> 00:11:12,000
you don't need double quotes, but in JSON, you do need these double quotes.
188
00:11:12,000 --> 00:11:14,500
Other than that, it's very straightforward.
189
00:11:14,500 --> 00:11:17,500
It's easy to read, which is great, because just looking at this,
190
00:11:17,500 --> 00:11:20,500
you can tell what it's representing, and you can tell what the different keys
191
00:11:20,500 --> 00:11:23,500
and values mean, and it's extremely lightweight.
192
00:11:23,500 --> 00:11:27,000
So when you send it across the internet through different APIs,
193
00:11:27,000 --> 00:11:30,500
it'll take up very little amount of space, which means it'll quickly send
194
00:11:30,500 --> 00:11:34,000
back and forth, which gives your user a great end experience.
195
00:11:34,000 --> 00:11:36,000
So thank you guys very much for watching.
196
00:11:36,000 --> 00:11:39,500
You now know everything you need to know in order to use JSON
197
00:11:39,500 --> 00:11:42,500
and consume JSON in your future projects.
198
00:11:42,500 --> 00:11:46,000
If you guys enjoyed this video, please make sure to leave me a like down below
199
00:11:46,000 --> 00:11:49,000
letting me know, and subscribe for more tutorials similar to this.
200
00:11:49,000 --> 00:11:53,000
Also, let me know down in the comments below what other style tutorials
201
00:11:53,000 --> 00:11:57,500
you'd like me to cover, similar to this one, in as short a manner as possible.
202
00:11:57,500 --> 00:12:00,000
Thank you guys very much for watching. Have a good day.
19448
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.