Display JSON data in HTML table using JavaScript

Sdílet
Vložit
  • čas přidán 8. 04. 2022
  • Hello everyone, in this video we are going to fetch data from a JSON file, and we are going to display them in an HTML table with JavaScript.
    Let me quick explain what we are going to do.
    We have some products stored in a JSON file. We are going to use the Fetch API to get those products from the JSON file. And we are going to put them in an HTML table.
    It's simple and easy.
    You can read the article and download the source code on my website:
    tinyurl.com/2ddzf96y
    If you feel like saying thanks, you can buy me a coffee.
    www.buymeacoffee.com/digitalfoxb
    If you like my videos press the subscribe button,
    it will help me a lot.
    Thank you.

Komentáře • 128

  • @christopherpink842
    @christopherpink842 Před 2 lety +12

    THANKS nicely explained straight to the point and easy to understand in less than 5 minutes compared to other 1 hour complicated youtube videos

  • @Bowlingkegel97
    @Bowlingkegel97 Před rokem +7

    This guy is the MAN! He is helpful, and has a good heart. He helped me with my problems and took the time for me. Subbed + Liked

  • @DigitalFox-tutorials
    @DigitalFox-tutorials  Před 2 lety +8

    Hi guys, i didn't mention in the video, that the script needs a local server like wamp, or xampp to run.
    If you more a reader, you can read the article and download the source code from my website:
    digitalfox-tutorials.com/tutorial.php?title=Fetch-data-stored-in-a-json-file-with-JavaScript

    • @unclegazza5783
      @unclegazza5783 Před 2 lety

      Can you show us how use Ajax to add the product into the shopping cart🤔

    • @aholiab4321
      @aholiab4321 Před 2 lety

      But the link is not working, I've been trying to download since yesterday

    • @DigitalFox-tutorials
      @DigitalFox-tutorials  Před 2 lety

      @@unclegazza5783 Yes soon

    • @DigitalFox-tutorials
      @DigitalFox-tutorials  Před 2 lety

      ​@@aholiab4321 Hey, it works i just tested it out, this is a link to the article on my website, you will find the download button at the end of the article.

    • @aholiab4321
      @aholiab4321 Před 2 lety

      @@DigitalFox-tutorials I got my firsnz to try it from here also, did you restrict Nigerian IP address to your website 😭😭.

  • @minimotivates
    @minimotivates Před 2 lety +3

    Very concise and clear, absolutely perfect explanation speed.

  • @curwenarthurs6901
    @curwenarthurs6901 Před 2 lety +8

    Excellent video. It helped me to easily understand how to dynamically create HTML using JS and to render it using the DOM. Thanks!

  • @SomeOne-rx2xw
    @SomeOne-rx2xw Před rokem +1

    very straightforward and well explained thanks man!

  • @rohitchowdhury2660
    @rohitchowdhury2660 Před rokem +1

    great it helped bro! I have been wondering for 2 days for this.

  • @crackrokmccaib
    @crackrokmccaib Před rokem +3

    Awesome job explaining this. You made it so easy to understand. I wish I could give more than one 👍.

  • @forester1
    @forester1 Před 2 lety +1

    Always a perfect tutorial.
    Thank you 💓

  • @bbhoxe
    @bbhoxe Před rokem

    That's a brilliant tutorial, thank you so much!

  • @shubhambargude
    @shubhambargude Před 2 lety +1

    Very simple and detailed tutorial

  • @Mahesh717
    @Mahesh717 Před 2 lety +1

    very helpful with clear explanation👍

  • @KHALEDMORSHED
    @KHALEDMORSHED Před rokem

    Well explained. It helps me lot. Manu many thanks brother.

  • @rajas2214
    @rajas2214 Před rokem

    thankyou so much, good explanation. i expected more videos in JSON

    • @DigitalFox-tutorials
      @DigitalFox-tutorials  Před rokem +1

      Thank you.
      I have more videos on how to work with JSON files on my channel. Check them out:
      czcams.com/video/0Nw1GvtmblY/video.html
      czcams.com/video/TMAwyq14FUI/video.html

  • @siberiatrekking
    @siberiatrekking Před 4 měsíci

    Very clear tutorial, thanks!

  • @eduardotejeda
    @eduardotejeda Před rokem

    Thanks, man. Great explanation.

  • @absbi0000
    @absbi0000 Před 7 měsíci

    awesome tutorial. thank you so much man!

  • @gregrosenquist664
    @gregrosenquist664 Před 2 lety

    very basic and perfect for my learning requirements at the moment. Regards.

  • @tosaponintaraumnauy1053

    thank you very much sir. >>ขอบคุณมากๆเลยครับ

  • @Cadknowledge
    @Cadknowledge Před rokem

    subscribed your channel awesome content thanks bro this is what i am looking for

  • @user-rx5yz7hp6t
    @user-rx5yz7hp6t Před 5 měsíci

    Thanks A Lot

  • @AbbasAli-yj3fg
    @AbbasAli-yj3fg Před rokem

    love you man
    you saved my S 😅😅

  • @mauarou4172
    @mauarou4172 Před rokem

    nice thankYou

  • @FrankGP.com.
    @FrankGP.com. Před rokem

    thankyou for source code download !

  • @shridharshikalgar9641
    @shridharshikalgar9641 Před 2 lety

    Excellent

  • @niteshjaiswar7805
    @niteshjaiswar7805 Před 6 měsíci

    Helpful+++

  • @Sara-vk4sp
    @Sara-vk4sp Před rokem

    Thank you so much!!!

  • @javierguajardo1760
    @javierguajardo1760 Před 9 měsíci

    Nice thank you

  • @beticohernandez9536
    @beticohernandez9536 Před rokem

    No hiciste el editar y eliminar?

  • @ozydeus6867
    @ozydeus6867 Před rokem

    You are heaven sent! Exactly what I've been looking for. Do you have tutorial on how to add JSON entries from user input (Add/Update/Delete)?

    • @DigitalFox-tutorials
      @DigitalFox-tutorials  Před rokem

      Thank you, check this out czcams.com/video/HyyAg0cI5N8/video.html

    • @ozydeus6867
      @ozydeus6867 Před rokem +1

      @@DigitalFox-tutorials Is it possible using javascript only? I'm trying to work my way around github pages, php is not yet allowed to be hosted.

    • @DigitalFox-tutorials
      @DigitalFox-tutorials  Před rokem +1

      Sorry, you can't manipulate files with javascript, you need a server side language to do that.

    • @illuminatv2848
      @illuminatv2848 Před rokem +1

      @@DigitalFox-tutorials I see. So in order to add key pairs to JSON it has to be added manually? Cannot add via code except PHP?

    • @DigitalFox-tutorials
      @DigitalFox-tutorials  Před rokem

      I am saying that if you want to write data in a file you have to use a server-side language like PHP, or you can use node.js. But you can not use javascript alone for this.
      Check this out. czcams.com/video/lT0w3pzZz4w/video.html

  • @LordmkKING67
    @LordmkKING67 Před rokem +1

    Thankfully i didnt go all the way by myself, would have been huge. So thanks for simplyfying. It displays things as it was written ${something.name} instead of the actual data however..

  • @nicoleuruchima595
    @nicoleuruchima595 Před rokem

    THANKS!

  • @sebastianschomig8708
    @sebastianschomig8708 Před rokem +1

    hey is there a way to use .json offline? i want to make a app for mobile phone including a function like this. i need to load and store data from/to a .json to show in my index.html. the problem i got is, i want that application as an offline-app. do you have a solution for my problem?

    • @DigitalFox-tutorials
      @DigitalFox-tutorials  Před rokem

      Hey, yes you can. If the json file is on your applications root folder and not in a remote location i don't see any problem

  • @chansamonejk5379
    @chansamonejk5379 Před rokem

    thank you

  • @crackrokmccaib
    @crackrokmccaib Před rokem

    This doesn't work for me. I get this error
    from origin 'null' has been blocked by CORS policy: Cross origin requests are only supported for protocol schemes: http, data, isolated-app, chrome-extension, chrome, https, chrome-untrusted.

    • @DigitalFox-tutorials
      @DigitalFox-tutorials  Před rokem

      Hey, where do you run the code? Are you fetching the JSON data from a remote location?

  • @TurielD
    @TurielD Před rokem +1

    Hmm, I tried to recreate this, and I I get is a header line.
    Then i tried downloading your original code to run that - and all i get is the header line, no products.

    • @DigitalFox-tutorials
      @DigitalFox-tutorials  Před rokem

      Hey, do you run the code from inside your localhost server. If not it will not work.

  • @chlwodlr999
    @chlwodlr999 Před rokem

    very helpful video
    could you tell me the reason why you used queryselecter?
    im still in a basic step

    • @DigitalFox-tutorials
      @DigitalFox-tutorials  Před rokem +1

      Hey, with the querySelector method we can target any html element like we do with css.
      Check this out: developer.mozilla.org/en-US/docs/Web/API/Document/querySelector

  • @kindyletutorials7197
    @kindyletutorials7197 Před 10 měsíci

    1000000000000000000000 thanks bro.

  • @rajurastogi6822
    @rajurastogi6822 Před 2 lety +1

    how do I escape hyphen in Key name ? It's giving me reference error and string/bracket Notation is not working.

    • @DigitalFox-tutorials
      @DigitalFox-tutorials  Před 2 lety

      Let's say this is the JSON key, value pair: "product-name": "Orient Vita Pro 28"
      You access the key like this: ${product["product-name"]}. I hope this is what you'r asking.

    • @rajurastogi6822
      @rajurastogi6822 Před 2 lety

      @@DigitalFox-tutorials Thanks, this works.

  • @Johan87577
    @Johan87577 Před rokem +1

    I tried to follow along but even after downloading your source files, it still doesn't display any but the table (no data in the tables)

    • @DigitalFox-tutorials
      @DigitalFox-tutorials  Před rokem

      Hey, are you running the code from your local server?? If not, it will not work.

  • @IDrKongoI
    @IDrKongoI Před rokem

    Wow i searched Hours to find a Way and the Solution is in an 4min Video.
    Awesome thank you.
    Is there a Way to do this with mutiple jsons ? Tried promise.all but cant figure out, how to store each json separat.

    • @DigitalFox-tutorials
      @DigitalFox-tutorials  Před rokem +1

      Hey thanks, i am sure there is a way to fetch data from multiple JSON files, but i didn't done it so far, i will think this over and i will get back to you.

  • @Willydmq
    @Willydmq Před rokem

    Súper entendible amigo!! Pregunta? Si quiero que el último producto no salga que debo hacer?

    • @DigitalFox-tutorials
      @DigitalFox-tutorials  Před rokem +1

      Before the for-of loop you can say products.pop(). This will remove the last object from
      the products array.
      products.pop();
      for(let product of products){
      out += `

      ${product.name}
      ${product.price}
      ${product.inventory}
      ${product.productCode}
      `;
      }

    • @Willydmq
      @Willydmq Před rokem

      @@DigitalFox-tutorials súper te agradezco amigo 👊

  • @souradipmukherjee6986
    @souradipmukherjee6986 Před měsícem

    The data object is printing its property values in console but on the webpage its printing undefined.....why is that happening

    • @DigitalFox-tutorials
      @DigitalFox-tutorials  Před měsícem

      Hey, check the values in the for-of loop for any typos.
      ${product.name}
      ${product.price}
      ${product.inventory}
      ${product.productCode}

  • @giapnguyen6693
    @giapnguyen6693 Před 7 měsíci +1

    I've tried to put in the table and onchange function but it only work on the first row any solution:(

    • @DigitalFox-tutorials
      @DigitalFox-tutorials  Před 7 měsíci

      Hey, let me take a look at your code.

    • @giapnguyen6693
      @giapnguyen6693 Před 7 měsíci

      @@DigitalFox-tutorials
      function chon(){
      var x = document.getElementById("chon").value;
      document.getElementById("demo").innerHTML= x;
      };
      fetch("db/employees.json")
      .then(function(response){
      return response.json();
      })
      .then(function(employees){
      let placeholder = document.querySelector("#Employees-table");
      let out = "";
      for(let employee of employees){
      var n=0;
      n=n+1;
      out += `

      ${employee.stt}
      ${employee.name}
      ${employee.phone}
      ${employee.email}


      Thực tập
      Nhân viên
      Chuyên viên


      `;
      }
      placeholder.innerHTML = out;
      });

    • @DigitalFox-tutorials
      @DigitalFox-tutorials  Před 7 měsíci

      You are not closing the select element and the table cell that holds the select menu.

      Thực tập
      Nhân viên
      Chuyên viên

      Try again. Hope it works.

    • @giapnguyen6693
      @giapnguyen6693 Před 7 měsíci

      @@DigitalFox-tutorials tks i've tried it but it still doesnt work 😢

    • @DigitalFox-tutorials
      @DigitalFox-tutorials  Před 7 měsíci

      You are applying the same id in every select menu. Every id in a page must be unique.
      Try this:
      Remove the id from the select menu, and pass-in as an argument 'this.value' to capture the value.
      And change the chon() function like this:
      function chon(value){
      document.getElementById("demo").innerHTML= value;
      };
      If this is not working zip your files and send them to digitalfox.tutorials@gmail.com, so i can see the whole project.

  • @Bowlingkegel97
    @Bowlingkegel97 Před rokem +1

    my code is not working :( even after i download your files it just shows the table rows without data what am i doing wrong im on macbook i tried chrome EDIT: It works on mamp only.. which is weird. it does not work locally. what can i do

    • @DigitalFox-tutorials
      @DigitalFox-tutorials  Před rokem

      It's not weird, the script needs a local server to run. When we are dealing with http request we need a server to handle the request to serve us the data that we requested. In my opinion everything you build, even it's only html and css, you have to do it in your local server. Think about this: you have finished a simple website with html, css, and a little bit of javascript. Where do you upload it? On a server.

    • @Bowlingkegel97
      @Bowlingkegel97 Před rokem

      @@DigitalFox-tutorials Youre a gem thank you for responding also so quickly..!. May i contact you for a simple question regarding my assessment?

    • @DigitalFox-tutorials
      @DigitalFox-tutorials  Před rokem

      Yes, but i don't always have the time to respond right away.

  • @MrAgentjames
    @MrAgentjames Před rokem

    is there any link that should be posted in head section, cause when I open index.html in browser there is no table, only header

    • @DigitalFox-tutorials
      @DigitalFox-tutorials  Před rokem

      Hey, You have to run the files from your localhost server

    • @MrAgentjames
      @MrAgentjames Před rokem

      @@DigitalFox-tutorials I did. I've done already some small projects and wanted to try this table. Everything is in one folder, I open in from that folder, but no table in browser.
      I tried to add link from bootstrap, thinking that maybe code is from there and has to have link, but didn't help.

    • @DigitalFox-tutorials
      @DigitalFox-tutorials  Před rokem

      Download the source code to see if anything changes.
      digitalfox-tutorials.com/tutorial.php?title=Display-JSON-data-in-HTML-table-using-JavaScript

    • @MrAgentjames
      @MrAgentjames Před rokem

      @@DigitalFox-tutorials source code is the one I used and put everything in local folder. I didn't type code myself.
      I don't know what could be wrong. Is there anything that should be downloaded or linked in like bootstrap?

    • @DigitalFox-tutorials
      @DigitalFox-tutorials  Před rokem

      There is noting else. Are you sure you run the code from inside you localhost server?

  • @sanchitsharma2899
    @sanchitsharma2899 Před rokem

    Uncaught (in promise) TypeError: Cannot set properties of null (setting 'innerHTML')
    Its showing this error even I tried put the script tag below table tag but not working.

    • @DigitalFox-tutorials
      @DigitalFox-tutorials  Před rokem

      I think that you maybe have a type error somewhere in your code.
      Download the source code and try again.
      digitalfox-tutorials.com/tutorial.php?title=Display-JSON-data-in-HTML-table-using-JavaScript
      Do you run the code from your local server?

  • @drakencraft1
    @drakencraft1 Před rokem

    Hi, I downloaded your source file, but still I cant see json file in html, just lables. please help

    • @DigitalFox-tutorials
      @DigitalFox-tutorials  Před rokem

      You have to run the files from your local server. Read the comments below this will help you.

    • @drakencraft1
      @drakencraft1 Před rokem +1

      @@DigitalFox-tutorials ou, thank you for fast answer. Is there some way how to do it locally? I can pull .json file from database, but I want to show it in html?

    • @DigitalFox-tutorials
      @DigitalFox-tutorials  Před rokem

      Running the code locally means, running the code in your computer and not in a remote server. But in order to run the tutorial's code locally, you have to install a local server like mamp or xampp in your computer, and run the files from inside the local's server directory.

  • @lance_oquendo
    @lance_oquendo Před 11 měsíci

    What if the JSON file starts with object? for example
    {info:
    [
    {
    "id":1
    },
    {
    "id":2
    }
    ]
    }

    • @DigitalFox-tutorials
      @DigitalFox-tutorials  Před 10 měsíci

      In this case you have to loop through the "info" object like this:
      for(let product of products.info){
      // output products
      }
      Also you have to use quotes around "info".
      Thanks good question.

    • @lance_oquendo
      @lance_oquendo Před 10 měsíci +1

      ​@@DigitalFox-tutorialsthank you so much! I did it 🎉

  • @206010613
    @206010613 Před rokem

    Hey, I copied the code as it is on your site, but I still get the "Uncaught TypeError: Failed to fetch" the script.js. Just installed python do I need to install some libraries?

    • @larsiparsii
      @larsiparsii Před 7 měsíci

      I know it's a late reply, but why are you using Python? This is for JavaScript/HTML.

    • @206010613
      @206010613 Před 7 měsíci

      I don't understand @@larsiparsii

  • @abhyudayshukla8732
    @abhyudayshukla8732 Před rokem +1

    I tried with this code but not able to see html table

    • @DigitalFox-tutorials
      @DigitalFox-tutorials  Před rokem

      Hey maybe you got a typo in your code.
      Download the code from my website and test it out.
      digitalfox-tutorials.com/tutorial.php?title=Display-JSON-data-in-HTML-table-using-JavaScript

    • @abhyudayshukla8732
      @abhyudayshukla8732 Před rokem

      @@DigitalFox-tutorials No typo error I checked twice. It's not working.

    • @DigitalFox-tutorials
      @DigitalFox-tutorials  Před rokem

      Do you have a local server installed on your computer like WAMP or XAMPP. Or if you run the code on a live server in VS code it will not work.

    • @hdquemada
      @hdquemada Před rokem

      @@DigitalFox-tutorials How would I be able to get it to work on a local server? I am using this example to learn further how to work with html, css and js, and it would be very helpful to be able to run this locally. It looks like other might benefit from this too.

  • @user-ip2kr3tm6h
    @user-ip2kr3tm6h Před 2 měsíci

    GUY WHO CAN HELP ME GET FULL CODE

    • @DigitalFox-tutorials
      @DigitalFox-tutorials  Před 2 měsíci +1

      Get the code from my website:
      digitalfox-tutorials.com/tutorial.php?title=Display-JSON-data-in-HTML-table-using-JavaScript

  • @developer2526
    @developer2526 Před 4 měsíci

    css file

    • @DigitalFox-tutorials
      @DigitalFox-tutorials  Před 4 měsíci +1

      Get the source code from here:
      digitalfox-tutorials.com/tutorial.php?title=Display-JSON-data-in-HTML-table-using-JavaScript