Vite JS Tutorial: JSON Import 📝

beginner
23 min

Vite JS Tutorial: JSON Import 📝

Welcome to our comprehensive guide on JSON Import in Vite JS! In this tutorial, we'll dive deep into understanding how to work with JSON files in Vite, a modern and fast-growing front-end build tool. By the end of this lesson, you'll be able to import JSON data into your Vite projects and leverage it for real-world applications. 🎯

What is JSON? 📝

JSON (JavaScript Object Notation) is a lightweight data interchange format that is easy for humans to read and write and easy for machines to parse and generate. It is a text format that is completely language independent but uses conventions that are familiar to programmers who use C-family languages.

Why Use JSON in Vite JS? 💡

JSON is a versatile data format that can be used to store and transport data between a server and a client (like a web browser). In Vite, JSON files are often used to store configuration data, data from APIs, or even predefined data structures for your application.

Setting Up a Vite Project 📝

Before we dive into JSON imports, let's quickly set up a new Vite project:

  1. Install Node.js (https://nodejs.org/en/download/)
  2. Install Vite globally: npm install -g vite
  3. Create a new Vite project: vite create my-app
  4. Navigate into the project folder: cd my-app
  5. Start the development server: vite

Importing JSON Files in Vite JS 🎯

Now that we have our Vite project set up, let's learn how to import JSON files:

  1. Create a new JSON file in the src folder: touch src/data.json
  2. Add some data to the JSON file:
json
{ "name": "John Doe", "age": 30, "hobbies": ["Reading", "Gaming", "Coding"] }
  1. Import the JSON data into a JavaScript file:
javascript
// src/main.js import { defineConfig } from 'vite' import { fileURLToPath } from 'url' import { readFileSync } from 'fs' // Import JSON data const data = JSON.parse(readFileSync(fileURLToPath(import.meta.url, '../data.json'))) console.log(data)
  1. Run the Vite development server: vite

You should now see the data from the JSON file printed to the console.

Real-World Example 🎯

Let's take it a step further and create a simple web page that displays data from our JSON file:

  1. Create a new App.vue file in the src folder: touch src/App.vue
  2. Add some basic HTML structure:
vue
<template> <div> <h1>Personal Info</h1> <ul> <li>Name: {{ name }}</li> <li>Age: {{ age }}</li> </ul> <h2>Hobbies</h2> <ul> <li v-for="hobby in hobbies">{{ hobby }}</li> </ul> </div> </template> <script> // Import JSON data import data from '../data.json' export default { data() { return { name: data.name, age: data.age, hobbies: data.hobbies } } } </script>
  1. Update the main.js file to use the Vue app:
javascript
import { createApp } from 'vue' import App from './App.vue' createApp(App).mount('#app')
  1. Modify the index.html file to include the Vue app:
html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <title>My Vite App</title> </head> <body> <div id="app"> <!-- Vue app will be rendered here --> </div> <!-- Imported Vue and Vite scripts --> <script type="module" src="/src/main.js"></script> </body> </html>
  1. Run the Vite development server: vite

Now, when you open your browser and navigate to http://localhost:5000, you should see the personal information and hobbies displayed from the JSON data.

Quiz 🎯

Quick Quiz
Question 1 of 1

What is the purpose of JSON files in Vite JS?

That's it for our JSON Import tutorial in Vite JS! With the knowledge you've gained today, you're ready to take on real-world projects and leverage JSON data in your Vite applications. Happy coding! 💡🎯💻