# Displaying JSON Data in a Table

In the previous tutorial, you loaded a JSON file and displayed basic information about the smart home. In this tutorial, you will display the device data in an HTML table using JavaScript.

---

## Current Project Structure

Your project should contain:

```text
json-demo
├── index.html
├── script.js
└── smart_security_data.json
```

---

## Create a Table Container

Open:

```text
index.html
```

Update the page so it contains:

```html
<!DOCTYPE html>
<html>
<head>
    <title>JSON Demo</title>
</head>
<body>

<h1>Smart Home Dashboard</h1>

<div id="output"></div>

<script src="script.js"></script>

</body>
</html>
```

The table will be generated inside the `output` div.

---

## Create the Table Structure

Open:

```text
script.js
```

Replace the previous code with:

```javascript
fetch("smart_security_data.json")
    .then(response => response.json())
    .then(data => {

        let html = `
            <h2>Devices</h2>

            <table border="1">

                <tr>
                    <th>Device ID</th>
                    <th>Device Type</th>
                    <th>Room</th>
                </tr>
        `;

        data.devices.forEach(device => {

            html += `
                <tr>
                    <td>${device.deviceId}</td>
                    <td>${device.deviceType}</td>
                    <td>${device.room}</td>
                </tr>
            `;

        });

        html += "</table>";

        document.getElementById("output").innerHTML = html;

    });
```

Save the file.

---

## View the Results

Open:

```text
http://localhost/json-demo/
```

You should see:

| Device ID | Device Type     | Room           |
| --------- | --------------- | -------------- |
| CAM-01    | Security Camera | Front Entrance |
| LOCK-02   | Smart Door Lock | Back Door      |



## Add Basic Styling

The table works, but it looks very plain.

Add the following CSS inside the `<head>` section of `index.html`:

```html
<style>

table {
    border-collapse: collapse;
    width: 100%;
}

th,
td {
    border: 1px solid #cccccc;
    padding: 10px;
    text-align: left;
}

th {
    background-color: #f2f2f2;
}

</style>
```

Refresh the page.

The table should now be easier to read.

## Display the Home Information Above the Table

Update the start of the HTML string:

```javascript
let html = `
    <p>
        <strong>Home ID:</strong>
        ${data.homeId}
    </p>

    <p>
        <strong>Location:</strong>
        ${data.location}
    </p>

    <h2>Devices</h2>

    <table border="1">

        <tr>
            <th>Device ID</th>
            <th>Device Type</th>
            <th>Room</th>
        </tr>
`;
```

The page should now display:

```text
Home ID: GC-HOME-014
Location: Gold Coast

Devices
```

followed by the table.

[![](https://mr.napper.au/uploads/images/gallery/2026-06/scaled-1680-/image-1780916592738.png)](https://mr.napper.au/uploads/images/gallery/2026-06/image-1780916592738.png)

## Complete JavaScript File

```javascript
fetch("smart_security_data.json")
    .then(response => response.json())
    .then(data => {

        let html = `
            <p>
                <strong>Home ID:</strong>
                ${data.homeId}
            </p>

            <p>
                <strong>Location:</strong>
                ${data.location}
            </p>

            <h2>Devices</h2>

            <table border="1">

                <tr>
                    <th>Device ID</th>
                    <th>Device Type</th>
                    <th>Room</th>
                </tr>
        `;

        data.devices.forEach(device => {

            html += `
                <tr>
                    <td>${device.deviceId}</td>
                    <td>${device.deviceType}</td>
                    <td>${device.room}</td>
                </tr>
            `;

        });

        html += "</table>";

        document.getElementById("output").innerHTML = html;

    });
```

You have successfully loaded JSON data and displayed it in an HTML table.

Next tutorial: **Displaying Nested Event Data from JSON**.