Welcome to a quick tutorial on how to create HTML lists from JSON data in Javascript. Want to dynamically generate a list from JSON data?
To create an HTML list from JSON data:
- Parse the JSON data into an array first (assuming it is a flat array) –
var data = JSON.parse(DATA);
- Loop through the array and create the HTML list.
var list = document.createElement("ul");
for (let i of data) { var item = document.createElement("li"); list.appendChild(item); }
- Lastly, append the list to where you want –
document.getElementById(ID).appendChild(list);
That should cover the basics, but read on for more examples!
ⓘ I have included a zip file with all the source code at the start of this tutorial, so you don’t have to copy-paste everything… Or if you just want to dive straight in.
TLDR – QUICK SLIDES
Fullscreen Mode – Click Here
TABLE OF CONTENTS
DOWNLOAD & NOTES
Firstly, here is the download link to the example code as promised.
QUICK NOTES
If you spot a bug, feel free to comment below. I try to answer short questions too, but it is one person versus the entire world… If you need answers urgently, please check out my list of websites to get help with programming.
EXAMPLE CODE DOWNLOAD
Click here to download all the example source code, I have released it under the MIT license, so feel free to build on top of it or use it in your own project.
HTML LIST FROM JSON DATA
All right, let us now get into the examples of how to create an HTML list from JSON data.
1) CREATE ELEMENT
<!-- (A) DIV CONTAINER -->
<div id="demoA"></div>
<!-- (B) JAVASCRIPT -->
<script>
// (B1) JSON STRING TO ARRAY
var data = '["Red","Green","Blue"]';
data = JSON.parse(data);
// (B2) CREATE LIST
var list = document.createElement("ol");
for (let i of data) {
let item = document.createElement("li");
item.innerHTML = i;
list.appendChild(item);
}
// (B3) APPEND LIST TO CONTAINER
document.getElementById("demoA").appendChild(list);
</script>
This is the “full version” of the snippet in the introduction, and it should be pretty self-explanatory:
- We use
JSON.parse()
to turn the JSON string back into an array. - Create the HTML list using
document.createElement("ol")
. - Then loop through the array, append the list items –
for (let i of data) { ... document.createElement("li") ... }
. - Lastly, add the list to where it is required –
document.getElementById(TARGET).appendChild(list)
.
2) MANUAL HTML STRING
<!-- (A) DIV CONTAINER -->
<div id="demoB"></div>
<!-- (B) JAVASCRIPT -->
<script>
// (B1) JSON STRING TO ARRAY
var data = '["Red","Green","Blue"]';
data = JSON.parse(data);
// (B2) CREATE LIST
var list = "<ul>";
for (let i of data) {
list += `<li>${i}</li>`;
}
list += "</ul>";
// (B3) APPEND LIST TO CONTAINER
document.getElementById("demoB").innerHTML = list;
</script>
Don’t like the “object-oriented” way of using createElement()
and appendChild()
? This is an alternative, where we create an HTML string instead.
3) NESTED OBJECTS & LISTS
<!-- (A) DIV CONTAINER -->
<div id="demoC"></div>
<!-- (B) JAVASCRIPT -->
<script>
// (B1) JSON STRING TO OBJECT
var data = '{"Fruits":["Durian","Elderberries","Feijoa"],"Vegetables":["Corn","Daikon","Eggplant"]}';
data = JSON.parse(data);
// (B2) CREATE LIST
var list = document.createElement("ul");
for (let i in data) {
// (B2-1) LIST ITEM
let item = document.createElement("li");
list.appendChild(item);
// (B2-2) SUB-SECTION TITLE
let head = document.createElement("strong");
head.innerHTML = i;
item.appendChild(head);
// (B2-3) SUB-SECTION ITEMS
let sublist = document.createElement("ul");
item.appendChild(sublist);
for (let j of data[i]) {
let subitem = document.createElement("li");
subitem.innerHTML = j;
sublist.appendChild(subitem);
}
}
// (B3) APPEND LIST TO CONTAINER
document.getElementById("demoC").appendChild(list);
</script>
Lastly, what if we have a nested object? Keep calm and look carefully, we are pretty much doing the same. Parse the JSON data into an object, then loop through the nested object to create a nested list.
EXTRA BITS & LINKS
That’s all for the tutorial, and here is a small section on some extras and links that may be useful to you.
LINKS & REFERENCES
INFOGRAPHIC CHEAT SHEET

THE END
Thank you for reading, and we have come to the end. I hope that it has helped you to better understand, and if you want to share anything with this guide, please feel free to comment below. Good luck and happy coding!