Convert JSON Data to HTML(problem with forEach method)

Convert JSON Data to HTML(problem with forEach method)


Tell us what’s happening:

Hello, coders! Can’t understand, why json.forEach method works in exercises but doesn’t work in codepen, for example? I do all the same, but get an error in console: json.forEach is not a function. And yes, I understand, that forEach is method for arrays, not for objects. I’m just confused, why it works in exercises then. It’s obviously, that data in lesson is not an array, but it is an object.

this problem appears during I create a random quote machine

Your code so far

  $(document).ready(function() {

    $("#getMessage").on("click", function() {
      $.getJSON("/json/cats.json", function(json) {

        var html = "";
        // Only change code below this line.
        json.forEach(function(val) {
          var keys = Object.keys(val);
          html += "<div class = 'cat'>";
          keys.forEach(function(key) {
            html += "<strong>" + key + "</strong>: " + val[key] + "<br>";
          html += "</div><br>";
        // Only change code above this line.



<div class="container-fluid">
  <div class = "row text-center">
    <h2>Cat Photo Finder</h2>
  <div class = "row text-center">
    <div class = "col-xs-12 well message">
      The message will go here
  <div class = "row text-center">
    <div class = "col-xs-12">
      <button id = "getMessage" class = "btn btn-primary">
        Get Message

Your browser information:

Your Browser User Agent is: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.89 Safari/537.36.

Link to the challenge:


The JSON that you get in the exercises happens to be an array, so forEach works there. If the data that you get from the API that you’re using is an array, you can use forEach as well.


how it can be array, if it looks like this:
id: 0
altText: A white cat wearing a green helmet shaped melon on it’s head.
codeNames: Juggernaut,Mrs. Wallace,Buttercup

Could you explain? I can’t understand. Array should looks like this [id, imageLink, altText, codeNames] etc. Isn’t it?


Each object looks like that, but the entire JSON is an array:


    "id": 0,
    "imageLink": "",
    "altText": "A white cat wearing a green helmet shaped melon on it's head. ",
    "codeNames": [
      "Mrs. Wallace",
    "id": 1,
    "imageLink": "",
    "altText": "A white cat with blue eys, looking very grumpy. ",
    "codeNames": [
    "id": 2,
    "imageLink": "",
    "altText": "A ginger cat with one eye closed and mouth in a grin-like expression. Looking very mischievous. ",
    "codeNames": [
      "The Doctor",


so, it will be good, if subscription in the random quote machine challenge mention it. Because of I’m very new in programming, I’ve spent a huge time for understanding, how to use unfited code from exercise…))) It would be better to get a hint, like this: “use your own way for this challenge, don’t pay attention to methods from exercises”.

And thanks for your help… You put me in a right way!)


The structure of data that you’ll get varies between different APIs. It helps to consult an API’s documentation before using it.


thanks, I’ll consider it!


in the array shown above, is the array considered the json, or is that an array of json objects?


In the example code at the top, the getJSON function requests the file containing the json. In the anonymous callback function, the sole argument called “json” contains the actual array. The external json file contains something that looks like a JavaScript array, but in the file it is just text. The conversion to an actual array is the performed by the getJSON function.