How to Loop through JSON in EJS ?
Last Updated :
24 Apr, 2025
EJS stands for Embedded JavaScript. It is a template engine designed to be used with server-side JavaScript environments like NodeJS and It is used to generate dynamic content in a web page.
In this article, we will learn about How to Loop through JSON in EJS with different Approaches.
Approaches to Create Loop through JSON in EJS:
We will go through different approaches to Loop through JSON Array and print the data using EJS. refer to the example section for examples of all approaches.
Using Standard For Loop:
<% for (let i = 0; i < data.length; i++) { %>
<%= data[i].JSON-key %>
<% } %>
Using Map Function:
<% data.map((item) => { %>
<%= item.JSON-key %>
<% }); %>
Using forEach Loop:
<% data.forEach((item) => { %>
<%= item.JSON-key %>
<% }); %>
Using for-in Loop:
<% for (let key in data) { %>
<% data[key].JSON-key -->
<% } %>
Steps to Create Node App & Install Required Modules:
Step 1: Create a NodeJS application using the following command:
npm init -y
Step 2: Install required Dependencies:
npm i ejs express
Project Structure:
Project StructureThe updated dependencies in package.json file will look like:
"dependencies": {
"ejs": "^3.1.9",
"express": "^4.18.2"
}
Example: The Below example is demonstrating the different approaches to Loop through JSON in EJS.
HTML
<!--File path: views/index.ejs -->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>EJS Example</title>
<style>
h1 {
color: green;
text-align: center;
}
.container{
display: flex;
justify-content: center;
gap:10px;
}
.box{
border: 1px solid black;
padding: 5px;
}
</style>
</head>
<body>
<h1>GeeksForGeeks | Loop through JSON Example</h1>
<div class="container">
<div class="box">
<b>Approach 1: Using Standard For Loop</b>
<% for (let i=0; i < courses.length; i++) { %>
<ul>
<li><b>Course name:</b>
<%= courses[i].name %>
</li>
<li><b>Course Category:</b>
<%= courses[i].category %>
</li>
<li><b>Topics:</b>
<ul>
<% for (let j=0; j < courses[i].topics.length; j++) { %>
<li>
<%= courses[i].topics[j] %>
</li>
<% } %>
</ul>
</li>
</ul>
<% } %>
</div>
<div class="box">
<b>Approach 2: Using Map Function</b>
<% courses.map((course)=>{ %>
<ul>
<li><b>Course name:</b>
<%= course.name %>
</li>
<li><b>Course Category:</b>
<%= course.category %>
</li>
<li><b>Topics:</b>
<ul>
<% course.topics.map((topic)=>{ %>
<li>
<%= topic %>
</li>
<% }) %>
</ul>
</li>
</ul>
<% }) %>
</div>
<div class="box">
<b>Approach 3: Using forEach Loop</b>
<% courses.forEach((course)=>{ %>
<ul>
<li><b>Course name:</b>
<%= course.name %>
</li>
<li><b>Course Category:</b>
<%= course.category %>
</li>
<li><b>Topics:</b>
<ul>
<% course.topics.forEach((topic)=>{ %>
<li>
<%= topic %>
</li>
<% }) %>
</ul>
</li>
</ul>
<% }) %>
</div>
<div class="box">
<b>Approach 4: Using for-in Loop</b>
<% for(let i in courses) { %>
<ul>
<li><b>Course name:</b>
<%= courses[i].name %>
</li>
<li><b>Course Category:</b>
<%= courses[i].category %>
</li>
<li><b>Topics:</b>
<ul>
<% for(let j in courses[i].topics) { %>
<li>
<%= courses[i].topics[j] %>
</li>
<% } %>
</ul>
</li>
</ul>
<% } %>
</div>
</div>
</body>
</html>
JavaScript
//File path: /app.js (root)
// Import required modules
const express = require('express');
const path = require('path');
// Create an Express application
const app = express();
// Define the port for the server to listen on
const port = 3000;
// Set EJS as the view engine
app.set('view engine', 'ejs');
// Set the views directory to 'views' in the current directory
app.set('views', path.join(__dirname, 'views'));
// Define a route to render the Pug template when the root path is accessed
app.get('/', (req, res) => {
//Sending this data from Server
const data = {
courses: [
{
name: 'Web Development',
category: 'Programming',
topics: ['HTML', 'CSS', 'JavaScript']
},
{
name: 'Java',
category: 'Programming',
topics: ['Introduction to Java', 'Object-Oriented Programming']
}
]
};
// Render the EJS template named 'index' and pass the data
res.render('index', data);
});
// Start the server and listen on the specified port
app.listen(port, () => {
// Display a message when the server starts successfully
console.log(`Server is running at http://localhost:${port}`);
});
To run the application use the following command:
node app.js
Output: Now go to http://localhost:3000 in your browser:

Conclusion:
EJS offers a easy mechanism for looping through JSON data within templates. By utilizing JavaScript code enclosed within <% %>
tags, developers can efficiently iterate over JSON arrays or objects. This capability enables dynamic rendering of HTML content based on JSON data, facilitating the creation of dynamic and interactive web applications
Similar Reads
How to Loop Through Array in jQuery?
jQuery offers various methods for looping through arrays, enabling operations like data transformation, filtering, and manipulation. Efficient array iteration is crucial for dynamic web applications and interactive content management.Below are the approaches to loop through an array in JQuery:Table
5 min read
How to Loop through XML in JavaScript ?
In JavaScript, looping over XML data is important for processing and extracting information from structured XML documents. Below is a list of methods utilized to loop through XML. Table of Content Using for loop with DOMParser and getElementsByTagNameUsing Array.from with DOMParser and childNodesUsi
2 min read
How to Loop Through an Array in JavaScript?
Here are the various ways to loop through an array in JavaScript1. Using for LoopThe for loop is one of the most used ways to iterate over an array. It gives you complete control over the loop, including access to the array index, which can be useful when you need to modify elements or perform other
4 min read
Loop through a JSON array in Python
A JSON array is an ordered list of values that can store multiple values such as string, number, boolean, or object. The values in a JSON array must be separated by commas and enclosed in squares in brackets []. In this article, we will learn how we can loop through a JSON array in Python. Iterate o
4 min read
How to open JSON file ?
In this article, we will open the JSON file using JavaScript. Â JSON stands for JavaScript Object Notation. It is basically a format for structuring data. The JSON format is a text-based format to represent the data in form of a JavaScript object.Approach:Create a JSON file, add data in that JSON fil
2 min read
How to write a for loop in ES6 ?
Loops are the way to do the same task again and again in a cyclic way. A loop represents a set of instructions that must be repeated. In a loopâs context, a repetition is termed an iteration. There are mainly two types of loops: Entry Controlled loops: In this type of loop the test condition is test
3 min read
How to Open JSON File?
JSON (JavaScript Object Notation) is a lightweight, text-based data format that stores and exchanges data. Let's see how we can create and open a JSON file.How to Create JSON Files?Before learning how to open a JSON file, it's important to know how to create one. Below are the basic steps to create
2 min read
How to use Ejs in JavaScript ?
EJS or Embedded Javascript Templating is a templating engine used by Node.js. The template engine helps to create an HTML template with minimal code. Also, it can inject data into the HTML template on the client side and produce the final HTML. Installation StepsInstall the module using the followin
3 min read
How to Loop through array of JSON object with *ngFor in Angular ?
JavaScript Object Notation (JSON) is a text-based, human-readable interchange format used for representing simple data structures and objects in web browser-based code. In order to Loop through an array of JSON objects, the *ngFor directive can be utilized that helps to dynamically generate HTML con
3 min read
How to write and use for loop in Vue js ?
Vue.js is one of the best frameworks for JavaScript like ReactJS. The VueJS is used to design the user interface layer, it is easy to pick up for any developer. It is compatible with other libraries and extensions as well. If you want to create a single-page application then VueJS is the first choic
4 min read