Importing JSON Files with Webpack
Overview of JSON Basics
In this lesson, we explored the fundamentals of JSON (JavaScript Object Notation), its syntax, and its practical applications. Below is a detailed guide:
What is JSON?
- JSON is a lightweight data-interchange format derived from JavaScript but used independently.
- It is commonly used for transferring data between a server and a client.
- JSON follows a strict syntax that ensures compatibility across systems and platforms.
Key JSON Rules and Structure
-
Basic Object Structure:
- JSON represents data as key-value pairs.
- Keys must be strings (enclosed in double quotes).
- Values can be:
- Strings:
"value" - Numbers:
123 - Booleans:
trueorfalse - Arrays:
[] - Objects:
{}
- Strings:
Example:
{ "name": "John Doe", "age": 30, "isStudent": false } -
Nested Objects:
- JSON allows nesting objects within objects.
Example:
{ "person": { "name": "John", "attributes": { "height": "6ft", "eyeColor": "blue" } } } - JSON allows nesting objects within objects.
-
Arrays in JSON:
- Arrays can hold multiple values of different types. Example:
{ "colors": ["red", "green", "blue"] }
Common JSON Errors
-
Keys must always be strings enclosed in double quotes:
- Correct:
"key": "value" - Incorrect:
key: "value"
- Correct:
-
Ensure proper commas between key-value pairs:
- Correct:
{ "key1": "value1", "key2": "value2" } - Incorrect:
{ "key1": "value1" "key2": "value2" }
- Correct:
-
JSON does not allow trailing commas:
- Correct:
{ "key1": "value1", "key2": "value2" } - Incorrect:
{ "key1": "value1", "key2": "value2", }
- Correct:
Next Steps
In the next lesson, we’ll explore how to integrate JSON files into your project using Webpack. Make sure your development environment is set up and your server is running before proceeding.
Video Transcript
00:01A bip.
00:01A big part of ES six is modular building, which enables us
00:05to put our code in a lot of different files
00:07and then bundle them up together.
00:10Unfortunately, JSON is not supported built into ES six,
00:14and for that we're going to use webpac
00:16and our Webpac dev server to add into it a loader.
00:21And that loader is gonna be the JSON loader.
00:23Now what's that that is gonna do is it's gonna expand the E
00:27ES six features to enable us to load in JSON files
00:30as we're compiling our application if we need data
00:33to be in our application as soon as it's loaded.
00:36So let's see it in action
00:37and let's see how to load up JSON file with the help
00:41of webpac.
00:43If we weren't working with webpac,
00:45we could have just natively used some no JS capabilities
00:50of loading in native
00:53JSON data using require,
00:56but us as our focus is on React,
00:59we really want to use webpac.
01:01And beyond that, we really want
01:02to use only E ES six standards.
01:05So with that said, let me show you how to get that running.
01:08And the first step I'm gonna do is I'm gonna go into my
01:11source folder and create a new file.
01:13I'm just gonna call this data dot J js o.
01:16And as we know how to create JSON files,
01:18I'm just gonna go ahead and delete the content
01:21and grab our object from within, leaving out that semi
01:25because it's just not needed or wanted
01:28or warranted in uh, JSO app.
01:32Now I'm gonna go ahead and save this.
01:34Now, before we could actually run our object, I'm gonna have
01:37to do here a few changes.
01:38So first of all, I'm gonna delete this object altogether
01:41and the next thing I'm gonna do is I'm going to
01:44terminate our terminal, uh, our server
01:47by clicking on command C or actually control, see,
01:51and I'm gonna exit because what I want to do is I want
01:54to go my NNPM install
01:56and I wanna install into my dev environment.
01:59So I'm gonna add dash dash, save dash dev
02:02and I'm gonna load up also JSON loader.
02:06Now, JSON Loader is an add-on that enables me to add
02:11customizations to the way webpac works,
02:14which it packages up my website.
02:17So I want to go to Webpac itself while it's installing into
02:20the Webpack config.
02:22And I'll just show you in the meantime that just
02:24as we have your, in our loaders, one
02:26of the loaders is the Babel loader,
02:28which manages the transcribing from ES six to ES five.
02:33I want to go ahead and add another loader only.
02:36This loader is gonna be in charge of loading in JSON files.
02:40Now to do that, I'm gonna go ahead and add a test.
02:43And this test is literally gonna be a RegX that tests out
02:47to see and this register, so lemme just copy it.
02:49It's gonna be very similar to any file
02:51that is a JS file only.
02:53Instead, we're gonna ask for any JS ON file, making sure
02:57that anything that is a
02:59Js o file will go through the specific loader
03:02that we're gonna account for here,
03:04which in our case it is called JSON.
03:08Now JSO for JSON Loader
03:09and Babel for Babel Loader,
03:11I could have typed here also JSON dash loader.
03:14By the way, I could have not put that dash loader as well.
03:16But what I'm literally doing here is I'm saying to webpac
03:20as it's packaging files, if it sees something that ends
03:23with json, take over, take over
03:26and you continue the implementation.
03:29Now, I could go inside of my client JS
03:33and in my code here, instead of creating a VAR file,
03:36I'm gonna use an E ES six standard.
03:39And this e es six standard is called import.
03:42And I'm gonna list out what do I want to import.
03:45In this case, I'm gonna call this data
03:47and I'm gonna import it from our folder, current folder
03:51into our data JSON.
03:54Now, because we had that web pack, it's gonna notice
03:57that it's A-J-S-O-N and it's gonna wrap up
03:59and do what it needs to and pass
04:01that information into the data folder.
04:04Now to test this out
04:05and see that it works, I'm gonna go ahead, you're an ask
04:07for data dot, uh, let's go for a name just
04:11so we'll have our, my name on the document.
04:14Last but not least, lemme go save this.
04:15And I just wanna show you that as we install this new
04:17package, you'll find it now inside
04:19of our JSO loader is gonna be at one
04:21of those web dependencies.
04:24With that said, let's go ahead into our start our server.
04:29So let just clear the screen.
04:31NPM start as we start up our server
04:35and it is running, if I go back into my browser, we'll see
04:39that my name is Ben and it sure is.
04:41Now, this, by the way, is not a standard in E ES six
04:44because e ES six does not support, uh,
04:48import importation of JSON files.
04:52You could use different libraries and loaders,
04:54but it's not built into the package.
04:57Although the support itself for JSON is built in,
05:01into JavaScript in general,
05:02but the loading of A-J-S-O-N external JSO file using
05:05import is not a standard.
05:07And we're actually using this work around through webpac
05:11to enable us to use it using that loader, making sure
05:14that it recognizes that it's at inside of the client.
05:17It recognizes it's a JSO file,
05:19and then it fills the data object up with the data that's
05:22inside of data JSON.
05:24Now, there are ways to create this type of structure,
05:27but we wouldn't just put your code when using import.
05:31There is a standard to export things,
05:33which we'll talk about in a future lecture.
05:36But for now, this solved our problem for needing
05:38to load up our JSON file.
05:42In this lecture, we saw how to use the Webpack dev server
05:45to load up our JSON file in the next lecture.
05:49Now that we have JSON data inside of our application,
05:52it's time for us to build out an E ES six React component.
