How to Create Lists From JSON Data in Javascript – Simple Examples

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:

  1. Parse the JSON data into an array first (assuming it is a flat array) – var data = JSON.parse(DATA);
  2. 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); }
  3. 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.

 

 

REAL QUICK SLIDES

 

TABLE OF CONTENTS

Download & Notes JSON List Useful Bits & Links
The End

 

DOWNLOAD & NOTES

Firstly, here is the download link to the example code as promised.

 

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.

 

QUICK NOTES

If you spot a bug, please feel free to comment below. I try to answer 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.

 

 

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

1-create-element.html
<!-- (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

2-html-string.html
<!-- (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

3-nested.html
<!-- (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) {
  // LIST ITEM
  let item = document.createElement("li");
  list.appendChild(item);
 
  // SUB-SECTION TITLE
  let head = document.createElement("strong");
  head.innerHTML = i;
  item.appendChild(head);
 
  // 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.

 

 

USEFUL 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.

 

INFOGRAPHIC CHEAT SHEET

HTML List From JSON Data (Click To Enlarge)

 

LINKS & REFERENCES

 

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!

Leave a Comment

Your email address will not be published. Required fields are marked *