# Translating a JSON data to HTML Element

**URL:** <https://forum.freecodecamp.org/t/translating-a-json-data-to-html-element/137934>\
**Category:** Curriculum Help\
**Tags:** javascript\
**Created:** [July 24, 2017, 7:28am UTC](https://forum.freecodecamp.org/t/translating-a-json-data-to-html-element/137934 "2017-07-24T07:28:05Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![zzeroda](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/zzeroda/32/61921_2.png) [@zzeroda](https://forum.freecodecamp.org/u/zzeroda)\
**Post date:** [July 24, 2017, 7:28am UTC](https://forum.freecodecamp.org/t/translating-a-json-data-to-html-element/137934/1 "2017-07-24T07:28:06Z")

</div>

Hi There!  
I use slim and just started to learn a basic REST API.  
I am a stuck on how I could transform my JSON Response into a value inside HTML Page.

Here is the api endpoint example and the response.

 ![](https://global.discourse-cdn.com/freecodecamp/original/3X/b/8/b86bb896d2ce0555e14b96314b5b280e63d5fa1a.png)

So the question is, How can I use that json data to put into my HTML element.  
For instance inside a

-

---

<div class="post-metadata">

**Author:** ![typicaljoe111](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/typicaljoe111/32/39229_2.png) [@typicaljoe111](https://forum.freecodecamp.org/u/typicaljoe111)\
**Post date:** [July 24, 2017, 8:12am UTC](https://forum.freecodecamp.org/t/translating-a-json-data-to-html-element/137934/2 "2017-07-24T08:12:32Z")

</div>

Hi,

You should be searching for some sort of ‘Javascript Template Engine’. Generally JS template engines accept JSON data and render HTML from them. Frameworks like AngujarJS and VueJS also do this.

---

<div class="post-metadata">

**Author:** ![ppc](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/ppc/32/63349_2.png) [@ppc](https://forum.freecodecamp.org/u/ppc)\
**Post date:** [July 24, 2017, 12:02pm UTC](https://forum.freecodecamp.org/t/translating-a-json-data-to-html-element/137934/3 "2017-07-24T12:02:26Z")

</div>

It’s the same as any dynamic modification to a webpage - use DOM APIs to build the table from scratch or generate an HTML string to insert into the page - here’s the result of the code below

![](https://global.discourse-cdn.com/freecodecamp/original/3X/7/8/78796856d564a2bac44d715ee7db1de14d1ba3a5.png)

Here’s the basics for both approaches - I have a simple HTML page

```auto
<!doctype html>
<!-- jsontable.html -->
<head>
<title>JSON To Table</title>
<style>
table,td,th {
 border:solid 1px;
 border-collapse:collapse;
}
</style>
</head>
<body>
<h1>JSON To Table</h1>
<p><form>
  <button name=string type=button>Table Using HTML String</button>
  <button name=dom type=button>Table Using DOM API</button>
  <p><button name=clear type=button>Clear</button>
  <p><output name=out></output>
</form>
<script src=jsontable.js></script>
</body>
</html>

```

Here’s the javascript file referenced above - it’s split into multiple sections below - paste them in order in a single file

The basic idea is simple - convert the JSON string to an object - traverse the JSON object - create DOM structure as strings or objects - insert new DOM into page

The data is hardcoded but could come from anywhere

```javascript
// jsontable.js

document.addEventListener("DOMContentLoaded", ()=>{
  let jsonstr=`[
    {"id":"2", "first_name":"Sam","last_name":"Smith", "phone":"111-222-3333","email":"ssmith@yahoo.com","address":"33 Birch Rd","city":"Miami","state":"FL"},
    {"id":"3", "first_name":"Brad","last_name":"Traversy", "phone":"211-322-4333","email":"brad@test.com","address":"222 South St","city":"Portland","state":"FL"}
  ]`
  let jsonobj=JSON.parse(jsonstr)
  let form=document.querySelector("form")

```

This is the HTML string button handler - it loops over the array of objects - it builds an HTML string that is inserted into the output section - I use `insertAdjacentHTML` but `innerHTML` is fine too

```javascript
  form.string.addEventListener("click", ()=>{
    let out=form.out
    if(!jsonobj.length)
      return
    let tblstr="<table>"
    tblstr+="<caption>Table Using HTML String</caption>"
    tblstr+="<tr>"
    for(let prop in jsonobj[0]) {
      tblstr+=`<th>${prop}`
    }
    tblstr+=jsonobj.reduce((s, x) => {
      s+="<tr>"
      for(let prop in x) {
        s+=`<td>${x[prop]}`
      }
      return s
    }, "")
    tblstr+="</table>"
    out.insertAdjacentHTML("beforeend", "<p>" + tblstr)
  })

```

This is the DOM API button handler - it creates each element of the table using DOM APIs - the logic is the same as HTML string generation - the implementation is different

```javascript
  form.dom.addEventListener("click", ()=>{
    let out=form.out
    if(!jsonobj.length)
      return
    let tbl=document.createElement("table")
    tbl.createCaption().innerText="Table Using DOM API"
    let hdr=tbl.insertRow()
    for(let prop in jsonobj[0]) {
      hdr.appendChild(document.createElement("th")).innerText=prop
    }
    jsonobj.reduce((t, x) => {
      let row=t.insertRow()
      for(let prop in x) {
        row.insertCell().innerText=x[prop]
      }
      return t
    }, tbl)
    out.appendChild(document.createElement("p"))
    out.appendChild(tbl)
  })

```

This is the reset button to clear the output section

```javascript
  form.clear.addEventListener("click", ()=>form.out.innerHTML="")
})

```

---

<div class="post-metadata">

**Author:** ![lionel-rowe](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/lionel-rowe/32/41877_2.png) [@lionel-rowe](https://forum.freecodecamp.org/u/lionel-rowe)\
**Post date:** [July 24, 2017, 12:27pm UTC](https://forum.freecodecamp.org/t/translating-a-json-data-to-html-element/137934/4 "2017-07-24T12:27:04Z")

</div>

The answers above are both correct, but the most basic answer is that you just navigate it as you would any JavaScript object (or array of objects, object containing arrays, array of objects containing arrays and strings, or whatever).

The example JSON data in the OP is an array containing two objects (`[{...},{...}]`), each of which contains a number of key value pairs. So let’s say you want the `first_name` of the second object (index 1), you’d get it with `jsonData[1].first_name` (assuming you assigned the returned JSON data to the variable `jsonData`).

To insert that into the DOM, you’d do something like this:

```auto
document.querySelector('#myDivId').innerHTML = jsonData[1].first_name;

```

To do that for each returned object:

```auto
for (var i=0; i<jsonData.length; i++) {
  document.querySelectorAll('.myDivClass')[i].innerHTML = jsonData[i].first_name;
}

```

---

<div class="post-metadata">

**Author:** ![zzeroda](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/zzeroda/32/61921_2.png) [@zzeroda](https://forum.freecodecamp.org/u/zzeroda)\
**Post date:** [July 26, 2017, 10:08am UTC](https://forum.freecodecamp.org/t/translating-a-json-data-to-html-element/137934/5 "2017-07-26T10:08:22Z")

</div>

Thanks all, very contributive to my learning progress,  
I will have a look and check some solutions based on your suggestions,  
appreciated all the responses. 😉

---

<div class="post-metadata">

**Author:** ![Shubham](https://avatars.discourse-cdn.com/v4/letter/s/85e7bf/32.png) [@Shubham](https://forum.freecodecamp.org/u/Shubham)\
**Post date:** [September 4, 2017, 5:58am UTC](https://forum.freecodecamp.org/t/translating-a-json-data-to-html-element/137934/6 "2017-09-04T05:58:06Z")

</div>

what is let in above code  
i am using this code it shows error in eclipse as= Syntax error on token “let”, : expected after this token

---

<div class="post-metadata">

**Author:** ![amkayondo](https://avatars.discourse-cdn.com/v4/letter/a/858c86/32.png) [@amkayondo](https://forum.freecodecamp.org/u/amkayondo)\
**Post date:** [August 24, 2019, 9:12pm UTC](https://forum.freecodecamp.org/t/translating-a-json-data-to-html-element/137934/7 "2019-08-24T21:12:03Z")

</div>

@zzeroda I solved it, just convert the JSON with JSON.stringify(data) and also am using fetch, this post will help [**a-practical-es6-guide-on-how-to-perform-http-requests-using-the-fetch-api**](https://www.freecodecamp.org/news/a-practical-es6-guide-on-how-to-perform-http-requests-using-the-fetch-api-594c3d91a547/)

```auto
fetch('http://localhost:8800/')
    .then((res) => res.json())
    .then((data) => {
    loginBarner.innerHTML = `${JSON.stringify(data)}`;
    console.log(data);
    })

```

Hope this helps

---

<div class="post-metadata">

**Author:** ![ILM](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/ilm/32/363239_2.png) [@ILM](https://forum.freecodecamp.org/u/ILM)\
**Post date:** [January 25, 2021, 12:19pm UTC](https://forum.freecodecamp.org/t/translating-a-json-data-to-html-element/137934/8 "2021-01-25T12:19:47Z")

</div>

Closed on January 25, 2021, 12:19pm UTC


