#1 India's Top IT Training Institute
×

About Uncodemy

Know more about who we are and what we stand for.

About Us

About Us

Discover our mission to equip individuals with essential IT skills and industry expertise.

Read More →
UnCodeMy Gallery

UnCodeMy Gallery

Explore highlights from our IT training sessions and success stories.

Read More →
Mentors

Mentors

Meet the industry experts who train and guide you through your course.

Read More →
Free Resources

Free Resources

Free tutorials, interview questions, tools and calculators to help you learn.

Explore →
×

Free Resources

Learn, practice and plan your career — bilkul free.

Tutorials

Tutorials

Step-by-step written tutorials to learn any technology at your own pace.

Read More →
Free Counselling

Free Counselling

Book a free video counselling session and pick the right course for you.

Read More →
Online Compiler

Online Compiler

Write, run and test your code online — no setup required.

Read More →

Use JSON Data to Populate Dynamic Blog Sections

You can keep your blog's post list in one JSON file and let JavaScript build sections such as "Latest posts" or "Python tutorials" from it, so adding a post means editing data, not HTML.

This tutorial builds that in plain JavaScript: the JSON format, the fetch and render code, the exact output, and the SEO trade-off to know about.

A Complete Guide for Modern Web Developers

On a small static blog, the home page often repeats the same post cards in several places: a "Latest" strip, a category block, a "Featured" box. Copying HTML by hand into each one is slow and easy to get wrong. A cleaner approach is to store each post once in a JSON file and generate the sections from it.

Step 1: Design the JSON file

Keep two lists: the posts, and the sections you want to show. Each section says which category to include and how many posts to show. Save this as /data/posts.json:

{
  "sections": [
    { "id": "latest", "title": "Latest posts", "filter": "all", "limit": 3 },
    { "id": "python", "title": "Python tutorials", "filter": "python", "limit": 2 }
  ],
  "posts": [
    { "slug": "python-lists", "title": "Python Lists Explained",
      "category": "python", "date": "2026-10-02", "excerpt": "Create, slice and sort lists." },
    { "slug": "css-grid", "title": "CSS Grid in 10 Minutes",
      "category": "css", "date": "2026-10-20", "excerpt": "Two-dimensional layouts made simple." },
    { "slug": "python-dicts", "title": "Working with Python Dictionaries",
      "category": "python", "date": "2026-11-01", "excerpt": "Keys, values and common methods." },
    { "slug": "js-fetch", "title": "Using fetch() in JavaScript",
      "category": "javascript", "date": "2026-09-15", "excerpt": "Load data from an API." }
  ]
}

Dates use the ISO format YYYY-MM-DD on purpose: ISO dates sort correctly as plain strings, so you do not need to parse them.

Step 2: Add a container to the page

<div id="blog-sections"></div>
<script src="/js/sections.js" defer></script>

The defer attribute downloads the script without blocking the page and runs it after the HTML is parsed. If you are unsure about the difference, see async vs defer in JavaScript.

Step 3: Fetch the data and build the sections

async function loadSections() {
  const root = document.getElementById("blog-sections");
  root.textContent = "Loading posts...";
  try {
    const res = await fetch("/data/posts.json");
    if (!res.ok) throw new Error("HTTP " + res.status);
    const data = await res.json();
    root.textContent = "";

    // newest first; ISO date strings compare correctly
    const sorted = [...data.posts].sort((a, b) => b.date.localeCompare(a.date));

    for (const section of data.sections) {
      const posts = sorted
        .filter(p => section.filter === "all" || p.category === section.filter)
        .slice(0, section.limit);
      root.appendChild(renderSection(section, posts));
    }
  } catch (err) {
    root.textContent = "Could not load posts. Please refresh the page.";
    console.error(err);
  }
}

function renderSection(section, posts) {
  const wrap = document.createElement("section");
  wrap.id = section.id;

  const h2 = document.createElement("h2");
  h2.textContent = section.title;
  wrap.appendChild(h2);

  const list = document.createElement("ul");
  for (const p of posts) {
    const li = document.createElement("li");
    const a = document.createElement("a");
    a.href = "/blog/" + encodeURIComponent(p.slug);
    a.textContent = p.title;
    const date = document.createElement("small");
    date.textContent = " - " + p.date;
    const excerpt = document.createElement("p");
    excerpt.textContent = p.excerpt;
    li.append(a, date, excerpt);
    list.appendChild(li);
  }
  wrap.appendChild(list);
  return wrap;
}

document.addEventListener("DOMContentLoaded", loadSections);

What the page shows

After sorting, the order is: Working with Python Dictionaries (1 Nov), CSS Grid in 10 Minutes (20 Oct), Python Lists Explained (2 Oct), Using fetch() in JavaScript (15 Sep). The two sections therefore render as:

Latest posts
  Working with Python Dictionaries - 2026-11-01
  CSS Grid in 10 Minutes - 2026-10-20
  Python Lists Explained - 2026-10-02

Python tutorials
  Working with Python Dictionaries - 2026-11-01
  Python Lists Explained - 2026-10-02

To add a post, you add one object to posts. To add a new block, such as "CSS tips", you add one object to sections. No HTML changes are needed.

Why the code uses textContent, not innerHTML

It is tempting to build each card with a template string and innerHTML. That works, but if any title or excerpt contains HTML (for example, a title like Using <script> tags, or data from an external CMS), the browser will interpret it as markup. That can break your layout or open a cross-site scripting hole. Setting textContent always treats the value as plain text. encodeURIComponent does the same job for the slug inside the URL.

If you prefer writing markup, use a <template> element in the HTML, clone it with template.content.cloneNode(true), and fill the clone's fields with textContent.

Common problems

  • Nothing loads when you open the file directly. Browsers block fetch() on file:// pages. Serve the folder with a local server, for example python -m http.server 8000, and open http://localhost:8000.
  • "Unexpected token" error. The JSON is invalid, usually because of a trailing comma or single quotes. JSON needs double quotes and no trailing commas.
  • Old posts keep showing. The browser or CDN cached posts.json. Add a version query, such as posts.json?v=20261116, when you publish changes.
  • The page jumps when sections appear. Give the container a minimum height so the layout does not shift while data loads.

The SEO trade-off

Google can render JavaScript, but rendering may happen later than the first crawl, and other search engines and link-preview bots may not run your script at all. For navigation blocks like "Latest posts" that is usually fine, because each post still has its own static page. For content that must be indexed, generate the HTML at build time instead: a small script (or a static site generator such as Eleventy, Hugo or Astro) can read the same posts.json and write plain HTML. You keep the single source of data and lose the dependency on client-side JavaScript.

Where to go next

Once sections work, you can add a "Load more" button or infinite scrolling, as shown in this infinite scroll blog layout tutorial. If you want to store user preferences such as a chosen category, look at the localStorage to-do list project. For background on how the browser talks to servers, read what APIs are and how they work in web apps, and keep your code readable with clean code practices in JavaScript.

Uncodemy's Full Stack Development course teaches JavaScript, APIs and front-end frameworks through projects like this one.

Placed Students

Our Clients

Partners

...