Creating a JSON file to store your photos and their descriptions for an Astro webpage project is a straightforward process. Here’s a step-by-step guide to help you get started:
Step 1: Create the JSON File
-
Create a Directory for Your Data: In your Astro project, create a directory to store your JSON files. For example, you might create a data directory in the root of your project.
-
Create the JSON File: Inside the data directory, create a file named photos.json.
Step 2: Structure Your JSON Data
Your photos.json file should be structured to include the necessary information for each photo. Here’s an example of what the JSON file might look like:
[
{
"id": 1,
"title": "Sunset Over the Ocean",
"description": "A beautiful sunset over the calm ocean.",
"url": "/images/sunset.jpg",
"alt": "A serene sunset over the ocean"
},
{
"id": 2,
"title": "Mountain Landscape",
"description": "A majestic mountain landscape with snow-capped peaks.",
"url": "/images/mountain.jpg",
"alt": "A stunning view of a mountain range"
},
{
"id": 3,
"title": "City Skyline",
"description": "The bustling city skyline at night.",
"url": "/images/city.jpg",
"alt": "A vibrant city skyline at night"
}
]
Step 3: Load the JSON Data in Your Astro Component
-
Import the JSON File: In your Astro component, you can import the JSON file directly.
-
Use the Data: You can then use the imported data in your component to display the photos and their descriptions.
Here’s an example of how you might do this in an Astro component:
---
import photos from '../data/photos.json';
---
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Photo Gallery</title>
</head>
<body>
<h1>Photo Gallery</h1>
<div class="photo-gallery">
{photos.map(photo => (
<div class="photo-item" key={photo.id}>
<img src={photo.url} alt={photo.alt} />
<h2>{photo.title}</h2>
<p>{photo.description}</p>
</div>
))}
</div>
</body>
</html>
Step 4: Style Your Photo Gallery
You can add some basic CSS to style your photo gallery. Create a styles.css file and link it in your Astro component:
/* styles.css */
.photo-gallery {
display: flex;
flex-wrap: wrap;
gap: 20px;
}
.photo-item {
border: 1px solid #ddd;
padding: 10px;
text-align: center;
}
.photo-item img {
max-width: 100%;
height: auto;
}
Link the CSS file in your Astro component:
---
import photos from '../data/photos.json';
import '../styles.css';
---
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Photo Gallery</title>
</head>
<body>
<h1>Photo Gallery</h1>
<div class="photo-gallery">
{photos.map(photo => (
<div class="photo-item" key={photo.id}>
<img src={photo.url} alt={photo.alt} />
<h2>{photo.title}</h2>
<p>{photo.description}</p>
</div>
))}
</div>
</body>
</html>
Step 5: Run Your Astro Project
Finally, run your Astro project to see the photo gallery in action:
npm run dev
Open your browser and navigate to the URL provided by the development server to see your photo gallery.
That’s it! You’ve successfully created a JSON file for your photos and photo descriptions and integrated it into your Astro webpage project.