Function - stringify
stringify - serialises javascript objects to JSON
JavaScript also has a built in function for converting an object into a JSON string.
This is a convenient way of showing an object that might not be displayed correctly.
var myArray = [ [1,2] , [3,3] ]
console.log(JSON.stringify(myArray));
// Parse the JSON so we can access what we need.
var parsed = JSON.parse(json);
// Get the amount of objects inside 'watson_tone' so we can loop through each one.
var count = Object.keys(parsed.watson_tone).length;
// Make some strings to include in our output.
var tableHeader = "<table><tr><th>score</th><th>tone_id</th></tr>";
var tableContent = "";
// Loop through the JSON and output each row in to a string.
for(i = 0; i < count; i++) {
tableContent = tableContent + "<tr><td>" + parsed.watson_tone[i].score + "</td><td>" + parsed.watson_tone[i].tone_id + "</tr>"; }
var tableFooter = "</table>";
// Get div and output the HTML. You can include these HTML strings straight in to your emailText variable.
document.getElementById("your_table").innerHTML = tableHeader + tableContent + tableFooter;
© 2023 Better Solutions Limited. All Rights Reserved. © 2023 Better Solutions Limited TopPrevNext