user7233151
user7233151

Reputation:

pulling data from JSON file to HTML with Javascript

I am trying to pull data out of a JSON file to put on my website, I followed this guide: https://developer.mozilla.org/en-US/docs/Learn/JavaScript/Objects/JSON but nothing appears on my page.

I've put my code here: https://codepen.io/anon/pen/mMGjxK

My HTML:

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <title>Sandbox</title>
    </head>
    <body>
        This is a test.
        <section>
        </section>
        <script src="script.js"></script>
    </body>
</html>

My JS:

var section = document.querySelector('section');
var retrieveData = 'https://raw.githubusercontent.com/okayauco/flashcards/master/sandbox/vocab-sandbox.json';
var request = new XMLHttpRequest();
request.open('GET', retrieveData);
request.responseType = 'JSON';
request.send();
request.onload = function() {
    var vocabWords = request.response;
    showWords(vocabWords);
}
function showWords(jsonObj) {
    var words = jsonObj['vocabulary'];
    for (var i = 0; i < words.length; i++) {
        var theArticle = document.createElement('article');
        var inEnglish = document.createElement('p');
        var inRomaji = document.createElement('p');
        var inHiragana = document.createElement('p');
        var inKanji = document.createElement('p');
        inEnglish.textContent = words[i].English;
        inRomaji.textContent = words[i].Romaji;
        inHiragana.textContent = words[i].Hiragana;
        inKanji.textContent = words[i].Kanji;
    }

    theArticle.appendChild(inEnglish);
    theArticle.appendChild(inRomaji);
    theArticle.appendChild(inHiragana);
    theArticle.appendChild(inKanji);
    section.appendChild(theArticle);
}

My JSON:

{"vocabulary":[
{"English":"one", "Romaji":"ichi", "Hiragana":"ぃち", "Kanji":"⼀" },
{"English":"two", "Romaji":"ni", "Hiragana":"に", "Kanji":"ニ" },
{"English":"three", "Romaji":"san", "Hiragana":"さん", "Kanji":"三" },
{"English":"four", "Romaji":"yon", "Hiragana":"よん", "Kanji":"四" }
]}

Upvotes: 1

Views: 866

Answers (2)

Duc Filan
Duc Filan

Reputation: 7157

First, your json is invalid (last comma), but below code is used for getting the content using XMLHttpRequest. You should use a valid json source and try again. You can validate your json by https://jsonlint.com/

Second, you put the appendChild out of the for loop. Then it's not appendable. Check my fixed code.

var section = document.querySelector('section');

var xhttp = new XMLHttpRequest();
xhttp.responseType = 'JSON';
xhttp.onreadystatechange = function() {
    if (this.readyState == 4 && this.status == 200) {
      var vocabWords = this.response;
      document.getElementById("demo").innerHTML = vocabWords;
      showWords(JSON.parse(vocabWords.replace('"四" },', '"四" }'))); // <= This should work with a valid json format.
    }
};
xhttp.open("GET", "https://raw.githubusercontent.com/okayauco/flashcards/master/sandbox/vocab-sandbox.json", true);
xhttp.send();

function showWords(jsonObj) {
  var words = jsonObj['vocabulary'];

  for (var i = 0; i < words.length; i++) {
    var theArticle = document.createElement('article');
    var inEnglish = document.createElement('p');
    var inRomaji = document.createElement('p');
    var inHiragana = document.createElement('p');
    var inKanji = document.createElement('p');

    inEnglish.textContent = words[i].English;
    inRomaji.textContent = words[i].Romaji;
    inHiragana.textContent = words[i].Hiragana;
    inKanji.textContent = words[i].Kanji;

    theArticle.appendChild(inEnglish);
    theArticle.appendChild(inRomaji);
    theArticle.appendChild(inHiragana);
    theArticle.appendChild(inKanji);

    section.appendChild(theArticle);
  };
};
    This is a test. 
    <section> 
    </section> 

<div id="demo"></div>

Upvotes: 0

adda82
adda82

Reputation: 175

It is your JSON. Remove last comma.

{ "vocabulary": [{ "English": "one", "Romaji": "ichi", "Hiragana": "ぃち", "Kanji": "⼀" }, { "English": "two", "Romaji": "ni", "Hiragana": "に", "Kanji": "ニ" }, { "English": "three", "Romaji": "san", "Hiragana": "さん", "Kanji": "三" }, { "English": "four", "Romaji": "yon", "Hiragana": "よん", "Kanji": "四" } ] }

Upvotes: 1

Related Questions