0

I am trying to make a table which information is stored and updating(not appending) in a JSON file using JavaScript and HTML. The structure of my JSON data is as like as follows:

[{
    "A": {
        "name": "MAK",
        "height": 170,
        "place": "Canada"
    },
    "B": {
        "name": "Jak",
        "height": 164,
        "place": "JAPAN"
    },
    "C": {
        "name": "TAKO",
        "height": 152,
        "place": "ROMANIA"
    },
    "D": {
        "name": "NARO",
        "height": 192,
        "place": "Portugal"
    },
    "E": {
        "name": "Monako",
        "height": 138,
        "place": "Poland"
    }
}]

Some details about the provided JSON information:
1/ With a random interval JSON file will be updated(not append).
2/ Key A.B.C ..... is not fixed. They could be increased and decreased.
3/Same also for their properties/sub-key(here name, height, place). New properties could come or remove.
4/ Besides, I cannot assure that all the time all properties will be present for all key. That means some place could be vacant.
I have added my required output which I want to see in the table. Hope it will depict the scenario in a better way. One flaw I am seeing here that the (0,0) coordinates of the table is vacant. Should I fill it/ left vacant with any string(probably in any other question it will be solved. If here then obviously awesome)?

Approach I have taken
1/ I am new in JavaScript so at first I want to try if I can iterate over the whole object and print all the content. You can see this fiddle. It can print all but the formation of the table does not meet the requirements.
2/ I have taken help from this answer.

Any solution regarding how to make the table will be highly appreciable.

2
  • Please show what you've tried. Right now your question reads like you're setting us a task. Commented Dec 13, 2020 at 12:21
  • @Mitya I have included the fiddle link in the post. For you again the approach I have taken. And the required output from my side. Commented Dec 13, 2020 at 12:39

1 Answer 1

0

Here you'll find what you need, do the styling yourself:

function CreateTableFromJSON() {
    var myPersons = [{
        "A": {
            "name": "MAK",
            "height": 170,
            "place": "Canada"
        },
        "B": {
            "name": "Jak",
            "height": 164,
            "place": "JAPAN"
        },
        "C": {
            "name": "TAKO",
            "height": 152,
            "place": "ROMANIA"
        },
        "D": {
            "name": "NARO",
            "height": 192,
            "place": "Portugal"
        },
        "E": {
            "name": "Monako",
            "height": 138,
            "place": "Poland"
        }
    }]
    // EXTRACT VALUE FOR HTML HEADER. 
    // ('name', 'height', and 'place')
    var col = [];
    for (var i = 0; i < myPersons.length; i++) {
        for (var key in myPersons[i]) {
            if (col.indexOf(key) === -1) {
                col.push(key);
            }
        }
    }

    // CREATE DYNAMIC TABLE.
    var table = document.createElement("table");

    // CREATE HTML TABLE HEADER ROW USING THE EXTRACTED HEADERS ABOVE.

    var tr = table.insertRow(-1);                   // TABLE ROW.

    for (var i = 0; i < col.length; i++) {
        var th = document.createElement("th");      // TABLE HEADER.
        th.innerHTML = col[i];
        tr.appendChild(th);
    }

    // ADD JSON DATA TO THE TABLE AS ROWS.
    for (var i = 0; i < myPersons.length; i++) {

        tr = table.insertRow(-1);

        for (var j = 0; j < col.length; j++) {
            var tabCell = tr.insertCell(-1);
            tabCell.innerHTML = myPersons[i][col[j]];
        }
    }

    // FINALLY ADD THE NEWLY CREATED TABLE WITH JSON DATA TO A CONTAINER.
    var divContainer = document.getElementById("showData");
    divContainer.innerHTML = "";
    divContainer.appendChild(table);
}

Sign up to request clarification or add additional context in comments.

1 Comment

thanks for your suggestion but I couldn't see any o/p. Maybe I am not understanding the correct path to connect JS and HTML. I have tried this

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.