A JSON response may contain exactly the records you need while showing readers only a tangle of braces and quotes. To display JSON as HTML, parse the text, check its shape, and create elements for the fields you want to show. The example below turns a small array into a table; the choices of columns and labels are part of the conversion from JSON into an HTML view.
Convert JSON to HTML With a Table
Save this file as index.html and open it in a browser. It parses a JSON string, verifies that every item has the expected fields, and adds a row for each record. A status message explains an invalid input instead of leaving a half-built table.
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Project list</title>
<style>
body { font-family: sans-serif; margin: 2rem; }
table { border-collapse: collapse; }
th, td { border: 1px solid #888; padding: .5rem; text-align: left; }
</style>
</head>
<body>
<h1>Projects</h1>
<p id="status" role="status"></p>
<table>
<caption>Current projects</caption>
<thead><tr><th scope="col">Name</th><th scope="col">Tasks</th></tr></thead>
<tbody id="rows"></tbody>
</table>
<script>
const source = '[{"name":"Atlas","tasks":3},{"name":"Beacon","tasks":0}]';
const rows = document.querySelector("#rows");
const status = document.querySelector("#status");
function parseProjects(text) {
const value = JSON.parse(text);
const valid = Array.isArray(value) && value.every(item =>
item !== null && typeof item === "object" && !Array.isArray(item) &&
typeof item.name === "string" &&
Number.isInteger(item.tasks) && item.tasks >= 0
);
if (!valid) throw new Error("Expected an array of projects with a name and task count.");
return value;
}
function renderProjects(projects) {
const fragment = document.createDocumentFragment();
for (const project of projects) {
const row = document.createElement("tr");
const name = document.createElement("td");
const tasks = document.createElement("td");
name.textContent = project.name;
tasks.textContent = String(project.tasks);
row.append(name, tasks);
fragment.append(row);
}
rows.replaceChildren(fragment);
status.textContent = projects.length ? "" : "No projects to display.";
}
try {
renderProjects(parseProjects(source));
} catch (error) {
rows.replaceChildren();
status.textContent = `Could not display projects: ${error.message}`;
}
</script>
</body>
</html>
The browser renders two rows: Atlas has three tasks and Beacon has none. The caption and column headers describe the table to readers and assistive technology. The DocumentFragment collects the new rows before replaceChildren() replaces the old ones, so calling the renderer again does not append duplicates.
Parse first, then check the shape
JSON.parse() turns valid JSON text into a JavaScript value. It may return an object, an array, a string, a number, a boolean, or null; successful parsing does not promise the particular shape a page needs. The example expects an array of objects with a string name and a nonnegative integer tasks. It checks every record before creating rows.
To convert JSON to HTML reliably, treat malformed syntax and an unexpected shape as separate problems. A trailing comma or single-quoted property name in JSON text causes a parsing error. A valid value such as {"name":"Atlas"} parses successfully but fails this table’s array check. The catch block reports either failure; a production interface could use a friendlier message and log diagnostic details separately.
An empty array is valid here: it means there are no projects, so the status says so. Missing tasks, a negative count, or a string such as "3" fails validation. If your application accepts other representations, define that rule explicitly and normalize the value before rendering. For instance, an API contract might allow null for a count that is not yet known. In that case, validate null as a distinct state and display “Unknown,” rather than conflating it with zero. The visual distinction tells a reader whether the project has no tasks or the count has not been supplied. Do not silently treat a missing count as zero unless that is the data contract.
Create elements for the view
The loop maps each project to a tr with two td elements. The values go into textContent, which treats a name like <strong>Atlas</strong> as text rather than interpreting it as markup. This matters when names come from users or an external service: inserting data through innerHTML can create an injection risk. The table structure is created by your code, while the record supplies only cell text.
When you turn JSON into an HTML table, you choose which properties appear and in what order. A third property such as an internal ID can remain in the data without being shown. A nested list might instead become a separate list inside a cell, or a link to a detail page. Decide how readers will use the information before choosing the representation. To convert JSON to HTML for a different audience, consider a compact list rather than a table when each item has only one prominent label. A table is useful when readers compare the same fields across many records. Both views can start from the same validated array, and neither requires changing the source data.
This distinction also explains why a JSON to HTML view is not automatically reversible. A table may omit fields, format numbers, rename labels, or combine several values in one cell. Another table could represent the same source differently. If you need to preserve the original structure for later editing or export, retain the underlying data and define an explicit serialization format.
Use an API response
The example embeds a string so you can run it without a server. For a real endpoint, fetch() can retrieve data and response.json() can parse the response body. Check response.ok before parsing, then apply the same shape validation to the resulting value. Because response.json() already returns a parsed value, do not pass it through JSON.parse() again.
When converting JSON data to HTML from a network request, handle a failed request, an unsuccessful HTTP status, invalid JSON, and a shape mismatch. Show a useful state for each situation, and avoid leaving stale rows in place if they no longer represent the selected request. If requests can overlap, decide which response is allowed to update the view so an older response cannot overwrite newer results.
Large datasets need a different rendering strategy from a short example. Creating thousands of rows can slow the page and make it difficult to navigate. Pagination or a smaller visible subset lets readers find information while limiting the number of DOM nodes. Keep headers and labels meaningful even if the presentation changes. If the source includes more fields later, update the validation and rendering rules together. A column heading with no corresponding value is as confusing as a value placed under the wrong heading; checking the mapping makes the interface trustworthy.
Check the result
A JSON to HTML display should be checked with normal, empty, and invalid inputs. Change the sample string to [] and confirm that the empty-state message appears. Then omit a tasks field and confirm that the error appears with no stale rows. Finally, use a name containing angle brackets and check that the characters appear as text rather than becoming elements.
To extend the conversion of JSON into HTML, add a status string to each project, validate it, and create a matching header and cell. Save the file, reload it, and verify that both rows show the intended status and that a missing status is rejected.
