Skip to content

JSON Files

Using JSON Data Embedded in JavaScript Files

Section titled “Using JSON Data Embedded in JavaScript Files”

Besides fetching JSON data from an API or a .json file, you can also embed JSON-like data directly within a JavaScript (.js) file. This approach is suitable for small, static datasets that don’t change often.

This method involves defining JavaScript objects or arrays directly in a script file and then using them.

Instead of fetching menu.json, let’s create a file named menu-data.js containing the data and a function call.

// Define the menu data directly as a JavaScript array of objects
const menuData = [
{
"display": "JavaScript Basics",
"url": "/tutorials/javascript/basics"
},
{
"display": "HTML Fundamentals",
"url": "/tutorials/html/fundamentals"
},
{
"display": "CSS Styling",
"url": "/tutorials/css/styling"
}
];
// Immediately call the display function (defined in the HTML file)
// Ensure the function exists before this script runs
if (typeof displayMenu === 'function') {
displayMenu(menuData);
} else {
console.error('displayMenu function not found. Ensure menu-data.js is loaded after the function definition.');
}

The HTML file defines the container and the displayMenu function, then loads the menu-data.js script.

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Embedded JSON Data Example</title>
</head>
<body>
<h1>Tutorial Menu (Embedded Data)</h1>
<div id="menuContainer"></div>
<p id="errorContainer" style="color: red;"></p>
<script>
// Define the function that will display the data
function displayMenu(items) {
const menuContainer = document.getElementById('menuContainer');
const errorContainer = document.getElementById('errorContainer');
// Clear any previous content/error
menuContainer.innerHTML = '';
errorContainer.textContent = '';
if (!Array.isArray(items)) {
errorContainer.textContent = 'Invalid menu data format provided.';
return;
}
// Create a list element
const ul = document.createElement('ul');
// Create list items for each menu item
items.forEach(item => {
const li = document.createElement('li');
const a = document.createElement('a');
a.href = item.url;
// Use textContent for security (prevents potential XSS if data were dynamic)
a.textContent = item.display;
li.appendChild(a);
ul.appendChild(li);
});
// Add the list to the container
menuContainer.appendChild(ul);
}
</script>
<!-- Load the JavaScript file containing the data and the function call -->
<!-- This script must be loaded *after* the displayMenu function is defined -->
<script src="menu-data.js"></script>
</body>
</html>
  1. Data Definition: The data (menuData) is defined as a standard JavaScript array of objects directly within menu-data.js.
  2. Function Definition: The displayMenu function, which knows how to render this data, is defined in the main HTML file’s script tag.
  3. Script Loading Order: Crucially, the menu-data.js script is included after the displayMenu function is defined. This ensures displayMenu exists when menu-data.js tries to call it.
  4. Execution: When menu-data.js loads, it defines the menuData constant and immediately calls displayMenu(menuData), passing the embedded data directly to the rendering function.
  • Simple for small, static datasets.
  • No need for an HTTP request to fetch the data, potentially faster initial load.
  • Data is bundled with the JavaScript code.
  • Data is mixed with code, which can be less maintainable.
  • Updating the data requires modifying the JavaScript file and potentially redeploying the script.
  • Not suitable for large datasets as it increases the initial script load size.
  • Cannot be easily consumed by other applications or services that expect a standard JSON API endpoint or file.
  • Requires careful management of script loading order.

This approach is less common than fetching JSON via HTTP, especially for dynamic data, but it can be useful for configuration or small, unchanging datasets within a specific JavaScript application.