I'm having trouble displaying my JSON data in HTML. I converted the JSON data into variables and created additional variables to access specific elements within the JSON data. However, when trying to pass this data to HTML using document.getElementById, only the last element is being printed as the Id is unique. Can someone please assist me with a solution for displaying JavaScript variable data in HTML?
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;
}
<h2>Convert a string written in JSON format, into a JavaScript object.</h2>
<p id="output"></p>
<p class="a1"></p>