# JavaScript to HTML

**URL:** <https://forum.freecodecamp.org/t/javascript-to-html/317529>\
**Category:** General\
**Created:** [September 26, 2019, 3:44pm UTC](https://forum.freecodecamp.org/t/javascript-to-html/317529 "2019-09-26T15:44:37Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![mohinish10](https://avatars.discourse-cdn.com/v4/letter/m/ed8c4c/32.png) [@mohinish10](https://forum.freecodecamp.org/u/mohinish10)\
**Post date:** [September 26, 2019, 3:44pm UTC](https://forum.freecodecamp.org/t/javascript-to-html/317529/1 "2019-09-26T15:44:37Z")

</div>

```auto

<!DOCTYPE html>
<html>
<body>

<h2>Convert a string written in JSON format, into a JavaScript object.</h2>

<p id="output"></p>
<p class="a1"></p>
<script>
var obj = [
    {
        "client_msg_id": "3a223f8d-b5aa-4c9c-9b63-045ec6f90b58",
        "type": "message",
        "text": "hey there",
        "source_team": "TN4AF0V5W",
        "team": "TN4AF0V5W",
        "user_profile": {
            "real_name": "marvelmohinish99",
            "team": "TN4AF0V5W"
        }
    },
    {
        "client_msg_id": "3a223f8d-b5aa-4c9c-9b63-045ec6f90b58",
        "type": "message",
        "text": "welcome",
        "source_team": "TN4AF0V5W",
        "team": "TN4AF0V5W",
        "user_profile": {
            "real_name": "marvelmohinish99",
            "team": "TN4AF0V5W"
        }
    },
    {
        "client_msg_id": "3a223f8d-b5aa-4c9c-9b63-045ec6f90b58",
        "type": "message",
        "text": "Help me",
        "source_team": "TN4AF0V5W",
        "team": "TN4AF0V5W",
        "user_profile": {
            "real_name": "marvelmohinish99",
            "team": "TN4AF0V5W"
        }
    }
];

for (var i in obj)
{
     var type = obj[i].type;

     var text= obj[i].text;

     var source_team = obj[i].source_team;

     var user_profile = obj[i].user_profile;

     var real_name = user_profile.real_name;

    console.log(source_team+" : "+real_name);

    console.log(text);

    document.getElementsByClassName('a1').innerHTML=text;	

}

</script>

	
</body>
</html>

```

Can anyone please help me to get the JSON data into HTML?

---

<div class="post-metadata">

**Author:** ![KittyKora](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/kittykora/32/124629_2.png) [@KittyKora](https://forum.freecodecamp.org/u/KittyKora)\
**Post date:** [September 26, 2019, 3:46pm UTC](https://forum.freecodecamp.org/t/javascript-to-html/317529/2 "2019-09-26T15:46:50Z")

</div>

Yes just follow this guide: [https://www.w3schools.com/js/js\_json\_html.asp](https://www.w3schools.com/js/js_json_html.asp)

---

<div class="post-metadata">

**Author:** ![mohinish10](https://avatars.discourse-cdn.com/v4/letter/m/ed8c4c/32.png) [@mohinish10](https://forum.freecodecamp.org/u/mohinish10)\
**Post date:** [September 26, 2019, 3:49pm UTC](https://forum.freecodecamp.org/t/javascript-to-html/317529/3 "2019-09-26T15:49:38Z")

</div>

Yes,but I don’t want to get JSON from URL.I have it embedded inside HTML itself.

---

<div class="post-metadata">

**Author:** ![KittyKora](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/kittykora/32/124629_2.png) [@KittyKora](https://forum.freecodecamp.org/u/KittyKora)\
**Post date:** [September 26, 2019, 3:55pm UTC](https://forum.freecodecamp.org/t/javascript-to-html/317529/4 "2019-09-26T15:55:08Z")

</div>

Sorry if it sounds dumb but shouldt you just then give it a proper path of your json file with JS?

---

<div class="post-metadata">

**Author:** ![snigo](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/snigo/32/128765_2.png) [@snigo](https://forum.freecodecamp.org/u/snigo)\
**Post date:** [September 26, 2019, 3:55pm UTC](https://forum.freecodecamp.org/t/javascript-to-html/317529/5 "2019-09-26T15:55:36Z")

</div>

Are you trying to rewrite `JSON.parse()` or you want us to point at it? I’m not sure…

---

<div class="post-metadata">

**Author:** ![mohinish10](https://avatars.discourse-cdn.com/v4/letter/m/ed8c4c/32.png) [@mohinish10](https://forum.freecodecamp.org/u/mohinish10)\
**Post date:** [September 26, 2019, 5:00pm UTC](https://forum.freecodecamp.org/t/javascript-to-html/317529/6 "2019-09-26T17:00:21Z")

</div>

Actually,I have created variables for each type of object inside array in JSON(ex- type,text,user\_profile) as you can see above. I used for loop to access all the elements inside the JSON. So,I think we don’t need to parse the data.Now,I want the data of variables(type,text,user\_profile) to be written/printed to HTML. To print the JS data to HTML we have document.getelementbyId but I couldn’t print out all the data to HTML with it as Id prints only one unique value. So,I want to know how to print the variable data from JS to HTML.

---

<div class="post-metadata">

**Author:** ![M-Michelini](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/m-michelini/32/109586_2.png) [@M-Michelini](https://forum.freecodecamp.org/u/M-Michelini)\
**Post date:** [September 27, 2019, 2:54am UTC](https://forum.freecodecamp.org/t/javascript-to-html/317529/7 "2019-09-27T02:54:42Z")

</div>

> [@How to get Javascript output into HTML](https://forum.freecodecamp.org/t/how-to-get-javascript-output-into-html/317647/4):
>
> document.getElementsByClassName(‘a1’).innerHTML=text;

getElementsByClassName returns an array of elements. if you want to select the p tag it will be the first element in that array, so:

```auto
document.getElementsByClassName('a1')[0].innerHTML=text;

```

but this will still be re writing the text each time.  
you need to append an item form each object. [HTML DOM Element appendChild() Method](https://www.w3schools.com/jsref/met_node_appendchild.asp)

---

<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 16, 2021, 6:01pm UTC](https://forum.freecodecamp.org/t/javascript-to-html/317529/8 "2021-01-16T18:01:14Z")

</div>

Closed on January 16, 2021, 6:01pm UTC


